mirror of
https://github.com/darmiel/yaxc.git
synced 2025-09-06 22:44:46 +08:00
made type of value to interface{}
again; use []byte
instead of string
to prevent race
This commit is contained in:
parent
6d769866da
commit
8631f67fcb
2 changed files with 23 additions and 20 deletions
|
@ -43,12 +43,14 @@ func (c *Cache) Set(key string, value interface{}, expiration time.Duration) {
|
|||
c.mu.Lock()
|
||||
|
||||
// TODO: remove debug
|
||||
fmt.Println(prefix,
|
||||
termenv.String("<-").Foreground(common.Profile().Color("#DBAB79")),
|
||||
"Set",
|
||||
termenv.String(key).Foreground(common.Profile().Color("#A8CC8C")),
|
||||
termenv.String("=").Foreground(common.Profile().Color("#DBAB79")),
|
||||
value)
|
||||
if b, o := value.([]byte); o {
|
||||
fmt.Println(prefix,
|
||||
termenv.String("<-").Foreground(common.Profile().Color("#DBAB79")),
|
||||
"Set",
|
||||
termenv.String(key).Foreground(common.Profile().Color("#A8CC8C")),
|
||||
termenv.String("=").Foreground(common.Profile().Color("#DBAB79")),
|
||||
common.PrettyLimit(string(b), 48))
|
||||
}
|
||||
|
||||
c.values[key] = &node{
|
||||
expires: c.expiration(expiration),
|
||||
|
@ -62,13 +64,14 @@ func (c *Cache) Get(key string) (interface{}, bool) {
|
|||
if v, o := c.values[key]; o && v != nil {
|
||||
if !v.expires.IsExpired() {
|
||||
|
||||
// TODO: remove debug
|
||||
fmt.Println(prefix,
|
||||
termenv.String("->").Foreground(common.Profile().Color("#66C2CD")),
|
||||
"Get",
|
||||
termenv.String(key).Foreground(common.Profile().Color("#A8CC8C")),
|
||||
termenv.String("=").Foreground(common.Profile().Color("#DBAB79")),
|
||||
v.value)
|
||||
if b, o := v.value.([]byte); o {
|
||||
fmt.Println(prefix,
|
||||
termenv.String("->").Foreground(common.Profile().Color("#66C2CD")),
|
||||
"Get",
|
||||
termenv.String(key).Foreground(common.Profile().Color("#A8CC8C")),
|
||||
termenv.String("=").Foreground(common.Profile().Color("#DBAB79")),
|
||||
common.PrettyLimit(string(b), 48))
|
||||
}
|
||||
|
||||
c.mu.Unlock()
|
||||
return v.value, true
|
||||
|
|
|
@ -11,27 +11,27 @@ type CacheBackend struct {
|
|||
}
|
||||
|
||||
func (b *CacheBackend) Get(key string) (res string, err error) {
|
||||
return b.get("val::" + key)
|
||||
return b.getString("val::" + key)
|
||||
}
|
||||
|
||||
func (b *CacheBackend) GetHash(key string) (res string, err error) {
|
||||
return b.get("hash::" + key)
|
||||
return b.getString("hash::" + key)
|
||||
}
|
||||
|
||||
func (b *CacheBackend) Set(key, value string, ttl time.Duration) error {
|
||||
b.cache.Set("val::"+key, value, ttl)
|
||||
b.cache.Set("val::"+key, []byte(value), ttl)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b *CacheBackend) SetHash(key, value string, ttl time.Duration) error {
|
||||
b.cache.Set("hash::"+key, value, ttl)
|
||||
b.cache.Set("hash::"+key, []byte(value), ttl)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b *CacheBackend) get(key string) (res string, err error) {
|
||||
func (b *CacheBackend) getString(key string) (res string, err error) {
|
||||
if v, ok := b.cache.Get(key); ok {
|
||||
if r, o := v.(string); o {
|
||||
res = r
|
||||
if r, o := v.([]byte); o {
|
||||
res = string(r)
|
||||
} else {
|
||||
err = b.errCast
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue