网站屏蔽ip,怎样在网站上做免费的网业,手机头像制作软件app,wordpress array和许多面向对象的编程语言一样#xff0c;Golang也存在interface接口这样的概念。interface相当于是一个中间层#xff0c;下游只需要关心interface实现了什么行为#xff0c;利用这些行为做些业务级别事情#xff0c;而上游则负责实现interface#xff0c;把这些行为具象…和许多面向对象的编程语言一样Golang也存在interface接口这样的概念。interface相当于是一个中间层下游只需要关心interface实现了什么行为利用这些行为做些业务级别事情而上游则负责实现interface把这些行为具象化。本文就来通过一个简单的缓存cache模块的实现来示范一下Golang的interface该怎么用。
首先从业务service角度而言一个cache模块可能需要以下几种方法
获取缓存中的某个值缓存数据加缓存时效删除缓存内容
那么这些个方法就可以用一类叫Cache的interface来表示
type Cache interface {Get(key string) (interface{}, bool)Set(key string, value interface{})SetExpire(key string, value interface{}, expire time.Duration)Delete(key string)
}其中Get方法返回一个interface{}的value以及是否存在的bool标识Set跟SetExpire表示无时限跟有时限的缓存行为Delete表示删除缓存内容。整块Cache的接口定义也非常明显。
这样写有什么好处如果你是下游业务服务的话你只需要这样写就可以了。这里给一个同package下的测试用例代码
func TestCache(t *testing.T) {k, v : hello, world// Current()的实现在下文慢慢解释var curCache Cache Current()// set get deletecurCache.Set(k, v)cached, ok : curCache.Get(k)if !ok {t.Fatalf(cannot cache %s:%s, k, v)} else {t.Logf(got cached %s:%v (type: %s), k, cached, reflect.TypeOf(cached).Name())}curCache.Delete(k)_, ok curCache.Get(k)if ok {t.Fatalf(cannot delete %s:%s, k, v)} else {t.Logf(delete cached %s:%s, k, v)}// set expirecurCache.SetExpire(k, v, 1*time.Second)cached, ok curCache.Get(k)if !ok {t.Fatalf(cannot cache %s:%s, k, v)} else {t.Logf(got cached %s:%v (type: %s), k, cached, reflect.TypeOf(cached).Name())}time.Sleep(3 * time.Second)_, ok curCache.Get(k)if ok {t.Fatalf(cannot expire %s:%s, k, v)} else {t.Logf(expired %s:%s, k, v)}
}可以看到我们指定的缓存对象curCache标识为一个Cache是个接口定义这样标识起来的话下面的代码就可以正常使用Get、Set之类的方法了。而更重要的是下面的代码不会因为Cache的具体实现变化而有所变化。举个例子你有10个开源的缓存库想定时切换Current() Cache背后的缓存对象实现就算你再怎么换只要用到缓存的代码标注缓存对象为Cache这个interface并且interface的定义没有变化那么使用缓存的代码就不需要动。这样就彻底实现了缓存提供方和使用方的解耦开发效率也会噌噌噌的上去。
既然提到了提供方Provider的概念那在缓存的实现上就可以走依赖注入控制反转的模式。假设某个Web服务有个本地缓存模块在实现上就可以考虑提供多个Cache接口的实现同时在配置里指定默认的一种。这里就以go-cache为例做一个实现案例。
import (github.com/patrickmn/go-cachetime
)const (GoCacheDefaultExpiration 10 * time.MinuteGoCacheCleanupInterval 15 * time.Minute
)type GoCache struct {c *cache.CachedefaultExpiration time.DurationcleanupInterval time.Duration
}func (g *GoCache) Get(key string) (interface{}, bool) {return g.c.Get(key)
}func (g *GoCache) Set(key string, value interface{}) {g.c.Set(key, value, GoCacheDefaultExpiration)
}func (g *GoCache) SetExpire(key string, value interface{}, expire time.Duration) {if expire 0 {expire g.defaultExpiration}if expire g.cleanupInterval {expire g.cleanupInterval}g.c.Set(key, value, expire)
}func (g *GoCache) Delete(key string) {g.c.Delete(key)
}func NewGoCache() *GoCache {return GoCache{c: cache.New(GoCacheDefaultExpiration, GoCacheCleanupInterval),defaultExpiration: GoCacheDefaultExpiration,cleanupInterval: GoCacheCleanupInterval,}
}当我们定义一个GoCache的struct实现了Cache接口定义的所有行为那么GoCache的实例在Golang里就能够被标识为一个Cache接口实例。NewGoCache方法不仅是提供了一个GoCache的实例而在业务层面更是提供了一个Cache实例。因此我们可以简单用一个map来管理所有的Cache的构造器从而标识不同的缓存实现
func provideGoCache() Cache {return NewGoCache()
}var cacheProviders map[string]Cache{go-cache: provideGoCache(),
}const (DefaultCacheProvider go-cache
)func Get(provider string) Cache {c, ok : cacheProviders[provider]if !ok {return nil}return c
}func Default() Cache {return Get(DefaultCacheProvider)
}// 上文提到的样例代码就用了这个方法拿到go-cache实现的Cache接口实例
func Current() Cache {return Default()
}显而易见通过这样的一个代码组织不论是go-cache抑或是其它的Cache实现都可以集中管理并灵活取用。这便是interface在Golang编程中给我们带来的便利了。