cache

package module
v0.0.0-...-5269655 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 16, 2015 License: Apache-2.0 Imports: 10 Imported by: 0

README

cache

import

	"github.com/weisd/cache"
	_ "github.com/weisd/cache/redis"

Documentation

package cache

import (
	"testing"
)

func Test_TagCache(t *testing.T) {

	c, err := New(Options{Adapter: "memory"})
	if err != nil {
		t.Fatal(err)
	}

	// base use
	err = c.Put("da", "weisd", 300)
	if err != nil {
		t.Fatal(err)
	}

	res := c.Get("da")

	if res != "weisd" {
		t.Fatal("base put faield")
	}

	t.Log("ok")

	// use tags/namespace
	err = c.Tags([]string{"dd"}).Put("da", "weisd", 300)
	if err != nil {
		t.Fatal(err)
	}
	res = c.Tags([]string{"dd"}).Get("da")

	if res != "weisd" {
		t.Fatal("tags put faield")
	}

	t.Log("ok")

	err = c.Tags([]string{"aa"}).Put("aa", "aaa", 300)
	if err != nil {
		t.Fatal(err)
	}

	res = c.Tags([]string{"aa"}).Get("aa")

	if res != "aaa" {
		t.Fatal("not aaa")
	}

	t.Log("ok")

	// flush namespace
	err = c.Tags([]string{"aa"}).Flush()
	if err != nil {
		t.Fatal(err)
	}

	res = c.Tags([]string{"aa"}).Get("aa")
	if res != "" {
		t.Fatal("flush faield")
	}

	res = c.Tags([]string{"aa"}).Get("bb")
	if res != "" {
		t.Fatal("flush faield")
	}

	// still store in
	res = c.Tags([]string{"dd"}).Get("da")
	if res != "weisd" {
		t.Fatal("where ")
	}

	t.Log("ok")

}

echo Middleware

e := echo.New()
e.Use(cache.EchoCacher(cache.Options{Adapter: "redis", AdapterConfig: `{"Addr":":6379"}`, Section: "test", Interval: 5}))

e.Get("/test/cache/put", func(c *echo.Context) error {
	err := cache.Store(c).Put("name", "weisd", 10)
	if err != nil {
		return err
	}

	return c.String(200, "store ok")
})

e.Get("/test/cache/get", func(c *echo.Context) error {
	name := cache.Store(c).Get("name")

	return c.String(200, "get name %s", name)
})

e.Run(":1323")

Documentation

Index

Constants

View Source
const EchoCacheStoreKey = "EchoCacheStore"

Variables

This section is empty.

Functions

func EchoCacher

func EchoCacher(opt Options) echo.MiddlewareFunc

func EncodeSha1

func EncodeSha1(str string) string

func Register

func Register(name string, adapter CacheStore)

Register registers a adapter.

func Version

func Version() string

Types

type Cache

type Cache interface {
	CacheStore
	Tags(tags []string) Cache
}

func New

func New(options ...Options) (Cache, error)

func NewTagCache

func NewTagCache(store CacheStore, names ...string) Cache

func Store

func Store(value interface{}) Cache

type CacheStore

type CacheStore interface {
	// Put puts value into cache with key and expire time.
	Put(key, val string, timeout int64) error
	// Get gets cached value by given key.
	Get(key string) string
	// Delete deletes cached value by given key.
	Delete(key string) error
	// Incr increases cached int-type value by given key as a counter.
	Incr(key string) (int64, error)
	// Decr decreases cached int-type value by given key as a counter.
	Decr(key string) (int64, error)
	// IsExist returns true if cached value exists.
	IsExist(key string) bool
	// Flush deletes all cached data.
	Flush() error
	// StartAndGC starts GC routine based on config string settings.
	StartAndGC(opt Options) error
	// update expire time
	Touch(key string, expire int64) error
}

Cache is the interface that operates the cache data.

type Engine

type Engine struct {
	Opt Options
	// contains filtered or unexported fields
}

func (*Engine) Decr

func (this *Engine) Decr(key string) (int64, error)

func (*Engine) Delete

func (this *Engine) Delete(key string) error

func (*Engine) Flush

func (this *Engine) Flush() error

func (*Engine) Get

func (this *Engine) Get(key string) string

func (*Engine) Incr

func (this *Engine) Incr(key string) (int64, error)

func (*Engine) IsExist

func (this *Engine) IsExist(key string) bool

func (*Engine) Put

func (this *Engine) Put(key, val string, timeout int64) error

func (*Engine) StartAndGC

func (this *Engine) StartAndGC(opt Options) error

func (*Engine) Tags

func (this *Engine) Tags(tags []string) Cache

func (*Engine) Touch

func (this *Engine) Touch(key string, expire int64) error

type MemoryCacher

type MemoryCacher struct {
	// contains filtered or unexported fields
}

MemoryCacher represents a memory cache adapter implementation.

func NewMemoryCacher

func NewMemoryCacher() *MemoryCacher

NewMemoryCacher creates and returns a new memory cacher.

func (*MemoryCacher) Decr

func (c *MemoryCacher) Decr(key string) (int64, error)

Decr decreases cached int-type value by given key as a counter.

func (*MemoryCacher) Delete

func (c *MemoryCacher) Delete(key string) error

Delete deletes cached value by given key.

func (*MemoryCacher) Flush

func (c *MemoryCacher) Flush() error

Flush deletes all cached data.

func (*MemoryCacher) Forever

func (c *MemoryCacher) Forever(key, val string) error

put value into cache with key forever save

func (*MemoryCacher) Get

func (c *MemoryCacher) Get(key string) string

Get gets cached value by given key.

func (*MemoryCacher) Incr

func (c *MemoryCacher) Incr(key string) (int64, error)

Incr increases cached int-type value by given key as a counter.

func (*MemoryCacher) IsExist

func (c *MemoryCacher) IsExist(key string) bool

IsExist returns true if cached value exists.

func (*MemoryCacher) Put

func (c *MemoryCacher) Put(key, val string, expire int64) error

Put puts value into cache with key and expire time.

func (*MemoryCacher) StartAndGC

func (c *MemoryCacher) StartAndGC(opt Options) error

StartAndGC starts GC routine based on config string settings.

func (*MemoryCacher) Touch

func (c *MemoryCacher) Touch(key string, expire int64) error

update expire time

type MemoryItem

type MemoryItem struct {
	// contains filtered or unexported fields
}

MemoryItem represents a memory cache item.

type Options

type Options struct {
	// Name of adapter. Default is "memory".
	Adapter string
	// Adapter configuration, it's corresponding to adapter.
	AdapterConfig string
	// GC interval time in seconds. Default is 60.
	Interval int
	// key prefix Default is ""
	Section string
}

type TagCache

type TagCache struct {
	// contains filtered or unexported fields
}

func (*TagCache) Decr

func (this *TagCache) Decr(key string) (int64, error)

func (*TagCache) Delete

func (this *TagCache) Delete(key string) error

func (*TagCache) Flush

func (this *TagCache) Flush() error

func (*TagCache) Get

func (this *TagCache) Get(key string) string

func (*TagCache) Incr

func (this *TagCache) Incr(key string) (int64, error)

func (*TagCache) IsExist

func (this *TagCache) IsExist(key string) bool

func (*TagCache) Put

func (this *TagCache) Put(key, value string, expire int64) error

func (*TagCache) StartAndGC

func (this *TagCache) StartAndGC(opt Options) error

func (*TagCache) TaggedItemKey

func (this *TagCache) TaggedItemKey(key string) string

func (*TagCache) Tags

func (this *TagCache) Tags(tags []string) Cache

add Tags

func (*TagCache) Touch

func (this *TagCache) Touch(key string, expire int64) error

更新过期时间

type TagSet

type TagSet struct {
	// contains filtered or unexported fields
}

func NewTagSet

func NewTagSet(store CacheStore, names []string) *TagSet

func (*TagSet) AddNames

func (this *TagSet) AddNames(names []string)

func (*TagSet) GetNamespace

func (this *TagSet) GetNamespace() string

取命名空间

func (*TagSet) Reset

func (this *TagSet) Reset() error

刷新所有 tag key

func (*TagSet) ResetTag

func (this *TagSet) ResetTag(name string) string

重置key

func (*TagSet) SetNames

func (this *TagSet) SetNames(names []string)

func (*TagSet) TagId

func (this *TagSet) TagId(name string) string

取tag id

func (*TagSet) TagIds

func (this *TagSet) TagIds() []string

取所有的tagid

func (*TagSet) TagKey

func (this *TagSet) TagKey(name string) string

Tag key

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL