1
0
mirror of https://github.com/astaxie/beego.git synced 2024-12-23 04:10:49 +00:00
Beego/client/cache/memcache/memcache.go

184 lines
4.3 KiB
Go
Raw Normal View History

2020-07-22 14:50:08 +00:00
// Copyright 2014 beego Author. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package memcache for cache provider
//
// depend on github.com/bradfitz/gomemcache/memcache
//
// go install github.com/bradfitz/gomemcache/memcache
//
// Usage:
// import(
// _ "github.com/astaxie/beego/cache/memcache"
// "github.com/astaxie/beego/cache"
// )
//
// bm, err := cache.NewCache("memcache", `{"conn":"127.0.0.1:11211"}`)
//
// more docs http://beego.me/docs/module/cache.md
package memcache
import (
2020-10-04 14:11:28 +00:00
"context"
2020-07-22 14:50:08 +00:00
"encoding/json"
"errors"
"strings"
"time"
"github.com/bradfitz/gomemcache/memcache"
2020-07-29 13:56:19 +00:00
"github.com/astaxie/beego/client/cache"
2020-07-22 14:50:08 +00:00
)
// Cache Memcache adapter.
type Cache struct {
conn *memcache.Client
conninfo []string
}
2020-08-06 15:07:18 +00:00
// NewMemCache creates a new memcache adapter.
2020-07-22 14:50:08 +00:00
func NewMemCache() cache.Cache {
return &Cache{}
}
// Get get value from memcache.
2020-10-04 14:11:28 +00:00
func (rc *Cache) Get(ctx context.Context, key string) (interface{}, error) {
2020-07-22 14:50:08 +00:00
if rc.conn == nil {
if err := rc.connectInit(); err != nil {
2020-10-04 14:11:28 +00:00
return nil, err
2020-07-22 14:50:08 +00:00
}
}
if item, err := rc.conn.Get(key); err == nil {
2020-10-04 14:11:28 +00:00
return item.Value, nil
} else {
return nil, err
2020-07-22 14:50:08 +00:00
}
}
2020-08-06 15:07:18 +00:00
// GetMulti gets a value from a key in memcache.
2020-10-04 14:11:28 +00:00
func (rc *Cache) GetMulti(ctx context.Context, keys []string) ([]interface{}, error) {
2020-07-22 14:50:08 +00:00
var rv []interface{}
if rc.conn == nil {
if err := rc.connectInit(); err != nil {
2020-10-04 14:11:28 +00:00
return rv, err
2020-07-22 14:50:08 +00:00
}
}
mv, err := rc.conn.GetMulti(keys)
if err == nil {
for _, v := range mv {
rv = append(rv, v.Value)
}
}
2020-10-04 14:11:28 +00:00
return rv, err
2020-07-22 14:50:08 +00:00
}
2020-08-06 15:07:18 +00:00
// Put puts a value into memcache.
2020-10-04 14:11:28 +00:00
func (rc *Cache) Put(ctx context.Context, key string, val interface{}, timeout time.Duration) error {
2020-07-22 14:50:08 +00:00
if rc.conn == nil {
if err := rc.connectInit(); err != nil {
return err
}
}
item := memcache.Item{Key: key, Expiration: int32(timeout / time.Second)}
if v, ok := val.([]byte); ok {
item.Value = v
} else if str, ok := val.(string); ok {
item.Value = []byte(str)
} else {
return errors.New("val only support string and []byte")
}
return rc.conn.Set(&item)
}
2020-08-06 15:07:18 +00:00
// Delete deletes a value in memcache.
2020-10-04 14:11:28 +00:00
func (rc *Cache) Delete(ctx context.Context, key string) error {
2020-07-22 14:50:08 +00:00
if rc.conn == nil {
if err := rc.connectInit(); err != nil {
return err
}
}
return rc.conn.Delete(key)
}
2020-08-06 15:07:18 +00:00
// Incr increases counter.
2020-10-04 14:11:28 +00:00
func (rc *Cache) Incr(ctx context.Context, key string) error {
2020-07-22 14:50:08 +00:00
if rc.conn == nil {
if err := rc.connectInit(); err != nil {
return err
}
}
_, err := rc.conn.Increment(key, 1)
return err
}
2020-08-06 15:07:18 +00:00
// Decr decreases counter.
2020-10-04 14:11:28 +00:00
func (rc *Cache) Decr(ctx context.Context, key string) error {
2020-07-22 14:50:08 +00:00
if rc.conn == nil {
if err := rc.connectInit(); err != nil {
return err
}
}
_, err := rc.conn.Decrement(key, 1)
return err
}
2020-08-06 15:07:18 +00:00
// IsExist checks if a value exists in memcache.
2020-10-04 14:11:28 +00:00
func (rc *Cache) IsExist(ctx context.Context, key string) (bool, error) {
2020-07-22 14:50:08 +00:00
if rc.conn == nil {
if err := rc.connectInit(); err != nil {
2020-10-04 14:11:28 +00:00
return false, err
2020-07-22 14:50:08 +00:00
}
}
_, err := rc.conn.Get(key)
2020-10-04 14:11:28 +00:00
return err == nil, err
2020-07-22 14:50:08 +00:00
}
2020-08-06 15:07:18 +00:00
// ClearAll clears all cache in memcache.
2020-10-04 14:11:28 +00:00
func (rc *Cache) ClearAll(context.Context) error {
2020-07-22 14:50:08 +00:00
if rc.conn == nil {
if err := rc.connectInit(); err != nil {
return err
}
}
return rc.conn.FlushAll()
}
2020-08-06 15:07:18 +00:00
// StartAndGC starts the memcache adapter.
// config: must be in the format {"conn":"connection info"}.
// If an error occurs during connecting, an error is returned
2020-07-22 14:50:08 +00:00
func (rc *Cache) StartAndGC(config string) error {
var cf map[string]string
json.Unmarshal([]byte(config), &cf)
if _, ok := cf["conn"]; !ok {
return errors.New("config has no conn key")
}
rc.conninfo = strings.Split(cf["conn"], ";")
if rc.conn == nil {
if err := rc.connectInit(); err != nil {
return err
}
}
return nil
}
// connect to memcache and keep the connection.
func (rc *Cache) connectInit() error {
rc.conn = memcache.New(rc.conninfo...)
return nil
}
func init() {
cache.Register("memcache", NewMemCache)
}