1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-30 00:31:29 +00:00
Beego/cache/cache.go

104 lines
3.0 KiB
Go
Raw Normal View History

2014-08-18 08:41:43 +00:00
// Copyright 2014 beego Author. All Rights Reserved.
2014-07-03 15:40:21 +00:00
//
2014-08-18 08:41:43 +00:00
// 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
2014-07-03 15:40:21 +00:00
//
2014-08-18 08:41:43 +00:00
// http://www.apache.org/licenses/LICENSE-2.0
2014-07-03 15:40:21 +00:00
//
2014-08-18 08:41:43 +00:00
// 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.
2015-09-08 16:15:03 +00:00
// Package cache provide a Cache interface and some implemetn engine
2014-08-18 08:41:43 +00:00
// Usage:
//
// import(
// "github.com/astaxie/beego/cache"
// )
//
// bm, err := cache.NewCache("memory", `{"interval":60}`)
//
// Use it like this:
//
// bm.Put("astaxie", 1, 10 * time.Second)
2014-08-18 08:41:43 +00:00
// bm.Get("astaxie")
// bm.IsExist("astaxie")
// bm.Delete("astaxie")
2014-07-03 15:40:21 +00:00
//
2014-08-18 08:41:43 +00:00
// more docs http://beego.me/docs/module/cache.md
2013-04-22 10:56:30 +00:00
package cache
import (
"fmt"
2016-01-08 05:47:14 +00:00
"time"
2013-04-22 10:56:30 +00:00
)
2013-12-22 05:35:02 +00:00
// Cache interface contains all behaviors for cache adapter.
// usage:
// cache.Register("file",cache.NewFileCache) // this operation is run in init method of file.go.
2014-08-02 02:12:03 +00:00
// c,err := cache.NewCache("file","{....}")
// c.Put("key",value, 3600 * time.Second)
2013-12-22 05:35:02 +00:00
// v := c.Get("key")
//
// c.Incr("counter") // now is 1
// c.Incr("counter") // now is 2
// count := c.Get("counter").(int)
2013-04-22 10:56:30 +00:00
type Cache interface {
2013-12-22 05:35:02 +00:00
// get cached value by key.
2013-04-22 10:56:30 +00:00
Get(key string) interface{}
// GetMulti is a batch version of Get.
GetMulti(keys []string) []interface{}
2013-12-22 05:35:02 +00:00
// set cached value with key and expire time.
2016-01-08 05:47:14 +00:00
Put(key string, val interface{}, timeout time.Duration) error
2013-12-22 05:35:02 +00:00
// delete cached value by key.
2013-04-22 10:56:30 +00:00
Delete(key string) error
2013-12-22 05:35:02 +00:00
// increase cached int value by key, as a counter.
2013-07-16 11:05:44 +00:00
Incr(key string) error
2013-12-22 05:35:02 +00:00
// decrease cached int value by key, as a counter.
2013-07-16 11:05:44 +00:00
Decr(key string) error
// check if cached value exists or not.
2013-04-22 10:56:30 +00:00
IsExist(key string) bool
2013-12-22 05:35:02 +00:00
// clear all cache.
2013-04-22 10:56:30 +00:00
ClearAll() error
// start gc routine based on config string settings.
2013-04-22 10:56:30 +00:00
StartAndGC(config string) error
}
2016-01-17 16:18:21 +00:00
// Instance is a function create a new Cache Instance
type Instance func() Cache
2016-01-17 16:18:21 +00:00
var adapters = make(map[string]Instance)
2013-04-22 10:56:30 +00:00
// 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.
2016-01-17 16:18:21 +00:00
func Register(name string, adapter Instance) {
2013-04-22 10:56:30 +00:00
if adapter == nil {
panic("cache: Register adapter is nil")
}
2014-07-10 05:04:18 +00:00
if _, ok := adapters[name]; ok {
2013-04-22 10:56:30 +00:00
panic("cache: Register called twice for adapter " + name)
}
adapters[name] = adapter
}
2015-09-08 16:15:03 +00:00
// NewCache Create a new cache driver by adapter name and config string.
2013-12-22 05:35:02 +00:00
// config need to be correct JSON as string: {"interval":360}.
// it will start gc automatically.
func NewCache(adapterName, config string) (adapter Cache, err error) {
instanceFunc, ok := adapters[adapterName]
2013-04-22 10:56:30 +00:00
if !ok {
err = fmt.Errorf("cache: unknown adapter name %q (forgot to import?)", adapterName)
2014-07-10 05:04:18 +00:00
return
2013-04-22 10:56:30 +00:00
}
adapter = instanceFunc()
err = adapter.StartAndGC(config)
if err != nil {
2014-07-10 05:04:18 +00:00
adapter = nil
}
2014-07-10 05:04:18 +00:00
return
2013-04-22 10:56:30 +00:00
}