mirror of
https://github.com/astaxie/beego.git
synced 2024-12-27 18:45:47 +00:00
98 lines
2.8 KiB
Go
98 lines
2.8 KiB
Go
// 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.
|
|
|
|
// Usage:
|
|
//
|
|
// import(
|
|
// "github.com/astaxie/beego/cache"
|
|
// )
|
|
//
|
|
// bm, err := cache.NewCache("memory", `{"interval":60}`)
|
|
//
|
|
// Use it like this:
|
|
//
|
|
// bm.Put("astaxie", 1, 10)
|
|
// bm.Get("astaxie")
|
|
// bm.IsExist("astaxie")
|
|
// bm.Delete("astaxie")
|
|
//
|
|
// more docs http://beego.me/docs/module/cache.md
|
|
package cache
|
|
|
|
import (
|
|
"fmt"
|
|
)
|
|
|
|
// Cache interface contains all behaviors for cache adapter.
|
|
// usage:
|
|
// cache.Register("file",cache.NewFileCache()) // this operation is run in init method of file.go.
|
|
// c,err := cache.NewCache("file","{....}")
|
|
// c.Put("key",value,3600)
|
|
// v := c.Get("key")
|
|
//
|
|
// c.Incr("counter") // now is 1
|
|
// c.Incr("counter") // now is 2
|
|
// count := c.Get("counter").(int)
|
|
type Cache interface {
|
|
// get cached value by key.
|
|
Get(key string) interface{}
|
|
// GetMulti is a batch version of Get.
|
|
GetMulti(keys []string) []interface{}
|
|
// set cached value with key and expire time.
|
|
Put(key string, val interface{}, timeout int64) error
|
|
// delete cached value by key.
|
|
Delete(key string) error
|
|
// increase cached int value by key, as a counter.
|
|
Incr(key string) error
|
|
// decrease cached int value by key, as a counter.
|
|
Decr(key string) error
|
|
// check if cached value exists or not.
|
|
IsExist(key string) bool
|
|
// clear all cache.
|
|
ClearAll() error
|
|
// start gc routine based on config string settings.
|
|
StartAndGC(config string) error
|
|
}
|
|
|
|
var adapters = make(map[string]Cache)
|
|
|
|
// Register makes a cache adapter available by the adapter name.
|
|
// If Register is called twice with the same name or if driver is nil,
|
|
// it panics.
|
|
func Register(name string, adapter Cache) {
|
|
if adapter == nil {
|
|
panic("cache: Register adapter is nil")
|
|
}
|
|
if _, ok := adapters[name]; ok {
|
|
panic("cache: Register called twice for adapter " + name)
|
|
}
|
|
adapters[name] = adapter
|
|
}
|
|
|
|
// Create a new cache driver by adapter name and config string.
|
|
// config need to be correct JSON as string: {"interval":360}.
|
|
// it will start gc automatically.
|
|
func NewCache(adapterName, config string) (adapter Cache, err error) {
|
|
adapter, ok := adapters[adapterName]
|
|
if !ok {
|
|
err = fmt.Errorf("cache: unknown adapter name %q (forgot to import?)", adapterName)
|
|
return
|
|
}
|
|
err = adapter.StartAndGC(config)
|
|
if err != nil {
|
|
adapter = nil
|
|
}
|
|
return
|
|
}
|