近期在写 ActivedRouter项目的时候需求一个缓存模型,要求缓存模型支持不同驱动,例如:memory、file、redis、mysql,实现思路代码如下:
cache.go文件,定义缓存对外接口
//ActivedRouter
//Author:usher.yue
//Amail:usher.yue@gmail.com
//TencentQQ:4223665
//缓存驱动接口定义
//可以自定义扩展驱动类型 默认是 memory driverpackage cacheimport ("./driver"
)//cache接口声明
type Cacher interface {GetStorage() driver.ContainererSet(k string, v interface{})Get(k string) (interface{}, bool)Del(k string)Has(k string) bool
}//create memory cache
//type "file" or "memory"
func Newcache(cacheType string) Cacher {switch cacheType {case "memory":return &driver.CacheImpl{Driver: driver.NewMapContainer()}case "file":{return &driver.CacheImpl{Driver: driver.NewFileContainer()}}case "mysql":{return &driver.CacheImpl{Driver: driver.NewMysqlContainer()}}case "redis":{return &driver.CacheImpl{Driver: driver.NewRedisContainer()}}case "mongodb":{return &driver.CacheImpl{Driver: driver.NewMongoContainer()}}}return nil
}
driver.go 实现驱动,这里我只利用Map实现了,具体其他驱动可自行实现
//ActivedRouter
//Author:usher.yue
//Amail:usher.yue@gmail.com
//TencentQQ:4223665
//缓存驱动接口定义
//可以自定义扩展驱动类型 默认是 memory cachepackage driver//容器接口声明
type Containerer interface {PushKVPair(k, v interface{}) ContainererEraseKVPair(k interface{}) ContainererPushKVMaps(maps ...map[string]interface{}) ContainererResetKVPair(k string, v interface{}) ContainererResetOrAddKVPair(k string, v interface{}) ContainererResetKVPairs(kvMaps map[string]interface{}) ContainererResetOrAddKVPairs(kvMaps map[string]interface{}) ContainererExist(k interface{}) boolGetData() *map[string]interface{}
}//基于内存实现的缓存
type CacheImpl struct {Driver Containerer //数据缓存驱动
}func (self *CacheImpl) Exist(k interface{}) bool {return self.Driver.Exist(k)
}func (self *CacheImpl) GetStorage() Containerer {return self.Driver
}//set
func (self *CacheImpl) Set(k string, v interface{}) {self.Driver.PushKVPair(k, v)
}//get
func (self *CacheImpl) Get(k string) (interface{}, bool) {mapData := *self.Driver.GetData()val, ok := mapData[k]return val, ok
}//erase
func (self *CacheImpl) Del(k string) {self.Driver.EraseKVPair(k)
}//has
func (self *CacheImpl) Has(k string) bool {mapData := *self.Driver.GetData()_, ok := mapData[k]return ok
}//Map实现的内存驱动
type MapContainer struct {data map[string]interface{}
}//创建数据channer
func NewMapContainer() *MapContainer {return &MapContainer{data: make(map[string]interface{})}
}func (this *MapContainer) PushKVPair(k, v interface{}) Containerer {if key, ok := k.(string); !ok {panic("key必须是string类型!")} else {this.data[key] = v}return this
}func (this *MapContainer) Exist(k interface{}) bool {return true
}func (this *MapContainer) EraseKVPair(k interface{}) Containerer {if key, ok := k.(string); !ok {panic("key必须是string类型!")} else {delete(this.data, key)}return this
}
func (this *MapContainer) PushKVMaps(maps ...map[string]interface{}) Containerer {for _, itemMap := range maps {for itemKey, itemValue := range itemMap {this.PushKVPair(itemKey, itemValue)}}return this
}
func (this *MapContainer) ResetKVPair(k string, v interface{}) Containerer {if _, ok := this.data[k]; ok {this.data[k] = v}return this
}
func (this *MapContainer) ResetOrAddKVPair(k string, v interface{}) Containerer {this.data[k] = vreturn this
}func (this *MapContainer) ResetKVPairs(kvMaps map[string]interface{}) Containerer {for k, v := range kvMaps {if _, ok := this.data[k]; ok {this.data[k] = v}}return this
}func (this *MapContainer) ResetOrAddKVPairs(kvMaps map[string]interface{}) Containerer {for k, v := range kvMaps {this.data[k] = v}return this
}func (this *MapContainer) GetData() *map[string]interface{} {return &this.data
}