cache

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: May 7, 2019 License: Apache-2.0 Imports: 8 Imported by: 0

README

ThinkGo-Cache

ThinkGo-Cache is a cache library for Golang,it currently supports redis, memory, and can customize the store adapter.

Installation

go get github.com/thinkoner/thinkgo/cache

Usage

Basic Usage
import (
	"github.com/thinkoner/thinkgo/cache"
	"time"
)


var foo string 

// Create a cache with memory store
c, _ := cache.Cache(cache.NewMemoryStore("thinkgo"))

// Set the value
c.Put("foo", "thinkgo", 10 * time.Minute)

// Get the string associated with the key "foo" from the cache
c.Get("foo", &foo)

Retrieve & Store

Sometimes you may wish to retrieve an item from the cache, but also store a default value if the requested item doesn't exist. For example, you may wish to retrieve all users from the cache or, if they don't exist, retrieve them from the callback and add them to the cache. You may do this using the Remember method:

var foo int

cache.Remember("foo", &a, 1*time.Minute, func() interface{} {
	return "thinkgo"
})

License

This project is licensed under the Apache 2.0 license.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Register

func Register(name string, adapter Store) error

Register Register a cache adapter available by the adapter name.

Types

type MemoryStore

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

func NewMemoryStore

func NewMemoryStore(prefix string) *MemoryStore

NewStore Create a memory cache store

func (*MemoryStore) DeleteExpired

func (s *MemoryStore) DeleteExpired()

Delete all expired items from the cache.

func (*MemoryStore) Exist

func (s *MemoryStore) Exist(key string) bool

Exist check cache's existence in memory.

func (*MemoryStore) Flush

func (s *MemoryStore) Flush() error

Remove all items from the cache.

func (*MemoryStore) Forget

func (s *MemoryStore) Forget(key string) error

Forget Remove an item from the cache.

func (*MemoryStore) Get

func (s *MemoryStore) Get(key string, val interface{}) error

Get get cached value by key. func (s *Store) Get(key string) (interface{}, error) {

func (*MemoryStore) GetPrefix

func (s *MemoryStore) GetPrefix() string

GetPrefix Get the cache key prefix.

func (*MemoryStore) Put

func (s *MemoryStore) Put(key string, val interface{}, timeout time.Duration) error

Put set cached value with key and expire time.

func (*MemoryStore) SetPrefix

func (s *MemoryStore) SetPrefix(prefix string) *MemoryStore

SetPrefix Set the cache key prefix.

type RedisStore

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

func NewRedisStore

func NewRedisStore(pool *redis.Pool, prefix string) *RedisStore

NewStore Create a redis cache store

func (*RedisStore) Exist

func (s *RedisStore) Exist(key string) bool

Exist check cache's existence in redis.

func (*RedisStore) Flush

func (s *RedisStore) Flush() error

Remove all items from the cache.

func (*RedisStore) Forget

func (s *RedisStore) Forget(key string) error

Forget Remove an item from the cache.

func (*RedisStore) Get

func (s *RedisStore) Get(key string, val interface{}) error

Get get cached value by key.

func (*RedisStore) GetPrefix

func (s *RedisStore) GetPrefix() string

GetPrefix Get the cache key prefix.

func (*RedisStore) Put

func (s *RedisStore) Put(key string, val interface{}, timeout time.Duration) error

Put set cached value with key and expire time.

func (*RedisStore) SetPool

func (s *RedisStore) SetPool(pool *redis.Pool) *RedisStore

SetPool Get the redis pool.

func (*RedisStore) SetPrefix

func (s *RedisStore) SetPrefix(prefix string) *RedisStore

SetPrefix Set the cache key prefix.

type Repository

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

func Cache

func Cache(adapter interface{}) (*Repository, error)

NewCache Create a new cache by adapter name.

func NewRepository

func NewRepository(store Store) *Repository

func (*Repository) Add

func (r *Repository) Add(key string, val interface{}, timeout time.Duration) error

Add Store an item in the cache if the key does not exist.

func (*Repository) Clear

func (r *Repository) Clear() error

Clear Remove all items from the cache.

func (*Repository) Delete

func (r *Repository) Delete(key string) error

Delete Alias for the "Delete" method.

func (*Repository) Forget

func (r *Repository) Forget(key string) error

Forget Remove an item from the cache.

func (*Repository) Get

func (r *Repository) Get(key string, val interface{}) error

Get Retrieve an item from the cache by key.

func (*Repository) GetStore

func (r *Repository) GetStore() Store

GetStore Get the cache store implementation.

func (*Repository) Has

func (r *Repository) Has(key string) bool

Has Determine if an item exists in the cache.

func (*Repository) Pull

func (r *Repository) Pull(key string, val interface{}) error

Pull Retrieve an item from the cache and delete it.

func (*Repository) Put

func (r *Repository) Put(key string, val interface{}, timeout time.Duration) error

Put Store an item in the cache.

func (*Repository) Remember

func (r *Repository) Remember(key string, val interface{}, timeout time.Duration, callback func() interface{}) error

Remember Get an item from the cache, or store the default value.

func (*Repository) Set

func (r *Repository) Set(key string, val interface{}, timeout time.Duration) error

Set Store an item in the cache.

type Store

type Store interface {
	// Get get cached value by key.
	Get(key string, val interface{}) error

	// Put set cached value with key and expire time.
	Put(key string, val interface{}, timeout time.Duration) error

	// Exist check cache's existence in redis.
	Exist(key string) bool

	// Forget Remove an item from the cache.
	Forget(key string) error

	// Flush Remove all items from the cache.
	Flush() error
}

Jump to

Keyboard shortcuts

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