cache

package
v0.0.0-...-10dc113 Latest Latest
Warning

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

Go to latest
Published: Oct 29, 2017 License: MIT, MIT Imports: 18 Imported by: 5

README

cache

Middleware cache provides cache management for Macross. It can use many cache adapters, including memory, file, Redis.

import

    "github.com/macross-contrib/cache"
	_ "github.com/macross-contrib/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.Set("da", "weisd", 300)
	if err != nil {
		t.Fatal(err)
	}

	res := ""
	c.Get("da", &res)

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

	t.Log("ok")

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

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

	t.Log("ok")

	err = c.Tags([]string{"macross"}).Set("macross", "macross_contrib", 300)
	if err != nil {
		t.Fatal(err)
	}

	res = ""
	c.Tags([]string{"macross"}).Get("macross", &res)

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

	t.Log("ok")

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

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

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

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

	t.Log("ok")

}

Macross Middleware

package main

import (
	"fmt"

	"github.com/insionng/macross"
	"github.com/macross-contrib/cache"
	_ "github.com/macross-contrib/cache/redis"
)

func main() {

	v := macross.New()
	v.Use(cache.Cacher(cache.Options{Adapter: "redis", AdapterConfig: `{"Addr":"127.0.0.1:6379"}`, Section: "test", Interval: 5}))

	v.Get("/cache/put/", func(self *macross.Context) error {
		err := cache.Store(self).Set("name", "macross", 60)
		if err != nil {
			return err
		}

		return self.String("store okay")
	})

	v.Get("/cache/get/", func(self *macross.Context) error {
		var name string
		cache.Store(self).Get("name", &name)

		return self.String(fmt.Sprintf("get name %s", name))
	})

	v.Listen(":7777")
}

Documentation

Overview

Copyright 2016~2017 Insionng

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.

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.

Index

Constants

View Source
const MakrossCacheStoreKey = "MakrossCacheStore"

Variables

This section is empty.

Functions

func Cacher

func Cacher(opt ...Options) makross.Handler

func DecodeGob

func DecodeGob(data []byte, o *Item) error

func EncodeGob

func EncodeGob(i *Item) ([]byte, error)

func EncodeSha1

func EncodeSha1(str string) string

func IsExist

func IsExist(path string) bool

IsExist checks whether a file or directory exists. It returns false when the file or directory does not exist.

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 {
	// Set puts value into cache with key and expire time.
	Set(key string, val interface{}, timeout int64) error
	// Get gets cached value by given key.
	Get(key string, _val interface{}) error
	// 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, _val interface{}) error

func (*Engine) Incr

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

func (*Engine) IsExist

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

func (*Engine) Set

func (this *Engine) Set(key string, val interface{}, 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 FileCacher

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

FileCacher represents a file cache adapter implementation.

func NewFileCacher

func NewFileCacher() *FileCacher

NewFileCacher creates and returns a new file cacher.

func (*FileCacher) Decr

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

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

func (*FileCacher) Delete

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

Delete deletes cached value by given key.

func (*FileCacher) Flush

func (c *FileCacher) Flush() error

Flush deletes all cached data.

func (*FileCacher) Get

func (c *FileCacher) Get(key string, _val interface{}) error

Get gets cached value by given key.

func (*FileCacher) Incr

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

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

func (*FileCacher) IsExist

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

IsExist returns true if cached value exists.

func (*FileCacher) Set

func (c *FileCacher) Set(key string, val interface{}, expire int64) error

Set puts value into cache with key and expire time. If expired is 0, it will be deleted by next GC operation.

func (*FileCacher) StartAndGC

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

StartAndGC starts GC routine based on config string settings.

func (*FileCacher) Touch

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

update expire time

type Item

type Item struct {
	Val     interface{}
	Created int64
	Expire  int64
}

Item represents a cache item.

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 string, val interface{}) error

put value into cache with key forever save

func (*MemoryCacher) Get

func (c *MemoryCacher) Get(key string, _val interface{}) error

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) Set

func (c *MemoryCacher) Set(key string, val interface{}, expire int64) error

Set 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, _val interface{}) error

func (*TagCache) Incr

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

func (*TagCache) IsExist

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

func (*TagCache) Set

func (this *TagCache) Set(key string, value interface{}, 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