mirror of
https://github.com/darmiel/yaxc.git
synced 2025-10-06 05:16:57 +08:00
40 lines
809 B
Go
40 lines
809 B
Go
package server
|
|
|
|
import (
|
|
"github.com/darmiel/yaxc/internal/bcache"
|
|
"time"
|
|
)
|
|
|
|
type CacheBackend struct {
|
|
cache *bcache.Cache
|
|
errCast error
|
|
}
|
|
|
|
func (b *CacheBackend) Get(key string) (res string, err error) {
|
|
return b.getString("val::" + key)
|
|
}
|
|
|
|
func (b *CacheBackend) GetHash(key string) (res string, err error) {
|
|
return b.getString("hash::" + key)
|
|
}
|
|
|
|
func (b *CacheBackend) Set(key, value string, ttl time.Duration) error {
|
|
b.cache.Set("val::"+key, value, ttl)
|
|
return nil
|
|
}
|
|
|
|
func (b *CacheBackend) SetHash(key, value string, ttl time.Duration) error {
|
|
b.cache.Set("hash::"+key, value, ttl)
|
|
return nil
|
|
}
|
|
|
|
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
|
|
} else {
|
|
err = b.errCast
|
|
}
|
|
}
|
|
return
|
|
}
|