62 lines
1.0 KiB
Go
62 lines
1.0 KiB
Go
package cache
|
||
|
||
import (
|
||
"fmt"
|
||
"time"
|
||
)
|
||
|
||
const (
|
||
ATTR_EXPIRE = "expire"
|
||
ATTR_MGOLOG = "mgolog"
|
||
ATTR_INSERT = "insert"
|
||
|
||
OpenMgoLog = false //true表示不禁用Mgolog,即不传入WithDisabledMgoLog()时也表示禁用
|
||
)
|
||
|
||
type empty struct {
|
||
}
|
||
|
||
type OperationAttr struct {
|
||
Name string
|
||
Value interface{}
|
||
}
|
||
|
||
type OperationAttrs []*OperationAttr
|
||
|
||
func (this OperationAttrs) Find(name string) *InterfaceResult {
|
||
for _, v := range this {
|
||
if v.Name == name {
|
||
return NewInterfaceResult(v.Value, nil)
|
||
}
|
||
}
|
||
|
||
if OpenMgoLog {
|
||
return NewInterfaceResult(ATTR_MGOLOG, nil)
|
||
}
|
||
return NewInterfaceResult(nil, fmt.Errorf("Operationattrs not found err: %v", name))
|
||
}
|
||
|
||
//缓存过期时间设置
|
||
func WithExpire(t time.Duration) *OperationAttr {
|
||
return &OperationAttr{
|
||
Name: ATTR_EXPIRE,
|
||
Value: t,
|
||
}
|
||
}
|
||
|
||
//禁用Mgolog操作
|
||
func WithDisabledMgoLog() *OperationAttr {
|
||
return &OperationAttr{
|
||
Name: ATTR_MGOLOG,
|
||
Value: empty{},
|
||
}
|
||
}
|
||
|
||
//新记录插入操作
|
||
func WithND() *OperationAttr {
|
||
return &OperationAttr{
|
||
Name: ATTR_INSERT,
|
||
Value: empty{},
|
||
}
|
||
}
|