1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-27 10:41:32 +00:00
Beego/client/cache/cache.go

105 lines
3.2 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 cache provide a Cache interface and some implement engine
// 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)
// bm.Get("astaxie")
// bm.IsExist("astaxie")
// bm.Delete("astaxie")
//
// more docs http://beego.me/docs/module/cache.md
package cache
import (
2020-10-04 14:11:28 +00:00
"context"
2020-07-22 14:50:08 +00:00
"fmt"
"time"
)
// 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 * time.Second)
// v := c.Get("key")
//
// c.Incr("counter") // now is 1
// c.Incr("counter") // now is 2
// count := c.Get("counter").(int)
type Cache interface {
2020-08-06 15:07:18 +00:00
// Get a cached value by key.
2020-10-04 14:11:28 +00:00
Get(ctx context.Context, key string) (interface{}, error)
2020-07-22 14:50:08 +00:00
// GetMulti is a batch version of Get.
2020-10-04 14:11:28 +00:00
GetMulti(ctx context.Context, keys []string) ([]interface{}, error)
2020-08-06 15:07:18 +00:00
// Set a cached value with key and expire time.
2020-10-04 14:11:28 +00:00
Put(ctx context.Context, key string, val interface{}, timeout time.Duration) error
2020-08-06 15:07:18 +00:00
// Delete cached value by key.
2020-10-04 14:11:28 +00:00
Delete(ctx context.Context, key string) error
2020-08-06 15:07:18 +00:00
// Increment a cached int value by key, as a counter.
2020-10-04 14:11:28 +00:00
Incr(ctx context.Context, key string) error
2020-08-06 15:07:18 +00:00
// Decrement a cached int value by key, as a counter.
2020-10-04 14:11:28 +00:00
Decr(ctx context.Context, key string) error
2020-08-06 15:07:18 +00:00
// Check if a cached value exists or not.
2020-10-04 14:11:28 +00:00
IsExist(ctx context.Context, key string) (bool, error)
2020-08-06 15:07:18 +00:00
// Clear all cache.
2020-10-04 14:11:28 +00:00
ClearAll(ctx context.Context) error
2020-08-06 15:07:18 +00:00
// Start gc routine based on config string settings.
2020-07-22 14:50:08 +00:00
StartAndGC(config string) error
}
// Instance is a function create a new Cache Instance
type Instance func() Cache
var adapters = make(map[string]Instance)
// 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 Instance) {
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
}
2020-08-06 15:07:18 +00:00
// NewCache creates a new cache driver by adapter name and config string.
// config: must be in JSON format such as {"interval":360}.
// Starts gc automatically.
2020-07-22 14:50:08 +00:00
func NewCache(adapterName, config string) (adapter Cache, err error) {
instanceFunc, ok := adapters[adapterName]
if !ok {
err = fmt.Errorf("cache: unknown adapter name %q (forgot to import?)", adapterName)
return
}
adapter = instanceFunc()
err = adapter.StartAndGC(config)
if err != nil {
adapter = nil
}
return
}