1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- package cache
- import (
- "strings"
- "github.com/Unknwon/com"
- "github.com/bradfitz/gomemcache/memcache"
- "github.com/go-macaron/cache"
- )
- type MemcacheCacher struct {
- c *memcache.Client
- }
- func NewItem(key string, data []byte, expire int32) *memcache.Item {
- return &memcache.Item{
- Key: key,
- Value: data,
- Expiration: expire,
- }
- }
- func (c *MemcacheCacher) Put(key string, val interface{}, expire int64) error {
- return c.c.Set(NewItem(key, []byte(com.ToStr(val)), int32(expire)))
- }
- func (c *MemcacheCacher) Get(key string) interface{} {
- item, err := c.c.Get(key)
- if err != nil {
- return nil
- }
- return string(item.Value)
- }
- func (c *MemcacheCacher) Delete(key string) error {
- return c.c.Delete(key)
- }
- func (c *MemcacheCacher) Incr(key string) error {
- _, err := c.c.Increment(key, 1)
- return err
- }
- func (c *MemcacheCacher) Decr(key string) error {
- _, err := c.c.Decrement(key, 1)
- return err
- }
- func (c *MemcacheCacher) IsExist(key string) bool {
- _, err := c.c.Get(key)
- return err == nil
- }
- func (c *MemcacheCacher) Flush() error {
- return c.c.FlushAll()
- }
- func (c *MemcacheCacher) StartAndGC(opt cache.Options) error {
- c.c = memcache.New(strings.Split(opt.AdapterConfig, ";")...)
- return nil
- }
- func init() {
- cache.Register("memcache", &MemcacheCacher{})
- }
|