向大佬们求助
需求:在通用工厂和通用产品中增加缓存机制,根据缓存使具体工厂和具体产品沿用,比如通用工厂AbstractHardwareAbstractionLayer对GetProcessor加缓存机制,一分钟后过期,那么具体工厂LinuxHardwareAbstractionLayer中的GetProcessor以及其他具体工厂都应该在一分钟后过期,再次获取时,重新查询。通用产品同理,通用产品AbstractCentralProcessor对GetMaxFreq加缓存机制,一分钟后过期,那么具体产品LinuxCentralProcessor中的GetMaxFreq以及其他具体产品(WindowsCentralProcessor中的GetMaxFreq...)都应该在一分钟后过期,再次获取
抽象工厂
type HardwareAbstractionLayer interface {
GetProcessor() CentralProcessor
}
抽象产品
type CentralProcessor interface {
GetMaxFreq() int64
}
通用工厂,实现通用工厂方法
type AbstractHardwareAbstractionLayer struct{}
func (l *AbstractHardwareAbstractionLayer) GetProcessor() CentralProcessor {
return nil
}
具体工厂
type LinuxHardwareAbstractionLayer struct {
*AbstractHardwareAbstractionLayer
}
func (l *LinuxHardwareAbstractionLayer) GetProcessor() CentralProcessor {
cpu := new(LinuxCentralProcessor)
return cpu
}
通用产品,实现通用产品方法
type AbstractCentralProcessor struct{}
func (l *AbstractCentralProcessor) GetMaxFreq() int64 {
return -1
}
具体产品
type LinuxCentralProcessor struct {
*AbstractCentralProcessor
}
func (p *LinuxCentralProcessor) GetMaxFreq() int64 {
log.Println("queryMaxFreq called")
return rand.Int63()
}
缓存工具
type memoize struct {
sync.RWMutex
value interface{}
expirationTime time.Time
}
func Memoize(original func() interface{}) func() interface{} {
return MemoizeWithExpiration(original, -1)
}
func MemoizeWithExpiration(original func() interface{}, ttl time.Duration) func() interface{} {
m := &memoize{}
return func() interface{} {
m.RLock()
if ttl >= 0 && time.Now().Before(m.expirationTime) && m.value != nil {
defer m.RUnlock()
return m.value
}
m.RUnlock()
m.Lock()
defer m.Unlock()
if ttl >= 0 && time.Now().Before(m.expirationTime) && m.value != nil {
return m.value
}
m.value = original()
if ttl >= 0 {
m.expirationTime = time.Now().Add(ttl)
} else {
m.expirationTime = time.Time{}
}
return m.value
}
}
func DefaultExpiration() time.Duration {
return time.Minute
}
需求:在通用工厂和通用产品中增加缓存机制,根据缓存使具体工厂和具体产品沿用,比如通用工厂AbstractHardwareAbstractionLayer对GetProcessor加缓存机制,一分钟后过期,那么具体工厂LinuxHardwareAbstractionLayer中的GetProcessor以及其他具体工厂都应该在一分钟后过期,再次获取时,重新查询。通用产品同理,通用产品AbstractCentralProcessor对GetMaxFreq加缓存机制,一分钟后过期,那么具体产品LinuxCentralProcessor中的GetMaxFreq以及其他具体产品(WindowsCentralProcessor中的GetMaxFreq...)都应该在一分钟后过期,再次获取
抽象工厂
type HardwareAbstractionLayer interface {
GetProcessor() CentralProcessor
}
抽象产品
type CentralProcessor interface {
GetMaxFreq() int64
}
通用工厂,实现通用工厂方法
type AbstractHardwareAbstractionLayer struct{}
func (l *AbstractHardwareAbstractionLayer) GetProcessor() CentralProcessor {
return nil
}
具体工厂
type LinuxHardwareAbstractionLayer struct {
*AbstractHardwareAbstractionLayer
}
func (l *LinuxHardwareAbstractionLayer) GetProcessor() CentralProcessor {
cpu := new(LinuxCentralProcessor)
return cpu
}
通用产品,实现通用产品方法
type AbstractCentralProcessor struct{}
func (l *AbstractCentralProcessor) GetMaxFreq() int64 {
return -1
}
具体产品
type LinuxCentralProcessor struct {
*AbstractCentralProcessor
}
func (p *LinuxCentralProcessor) GetMaxFreq() int64 {
log.Println("queryMaxFreq called")
return rand.Int63()
}
缓存工具
type memoize struct {
sync.RWMutex
value interface{}
expirationTime time.Time
}
func Memoize(original func() interface{}) func() interface{} {
return MemoizeWithExpiration(original, -1)
}
func MemoizeWithExpiration(original func() interface{}, ttl time.Duration) func() interface{} {
m := &memoize{}
return func() interface{} {
m.RLock()
if ttl >= 0 && time.Now().Before(m.expirationTime) && m.value != nil {
defer m.RUnlock()
return m.value
}
m.RUnlock()
m.Lock()
defer m.Unlock()
if ttl >= 0 && time.Now().Before(m.expirationTime) && m.value != nil {
return m.value
}
m.value = original()
if ttl >= 0 {
m.expirationTime = time.Now().Add(ttl)
} else {
m.expirationTime = time.Time{}
}
return m.value
}
}
func DefaultExpiration() time.Duration {
return time.Minute
}