cache

package
v1.4.1 Latest Latest
Warning

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

Go to latest
Published: Feb 7, 2023 License: Apache-2.0 Imports: 9 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/forgoer/thinkgo/cache

Usage

Basic Usage
import (
	"github.com/forgoer/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

View Source
const ReferenceKeyForever = "forever_ref"

ReferenceKeyForever Forever reference key.

View Source
const ReferenceKeyStandard = "standard_ref"

ReferenceKeyStandard Standard reference key.

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.

func Sha1 added in v1.2.0

func Sha1(str string) string

Sha1 Calculate the sha1 hash of a string

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) Decrement added in v1.0.1

func (s *MemoryStore) Decrement(key string, value ...int) (int, error)

Decrement the value of an item in the cache.

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) Expire added in v1.0.1

func (s *MemoryStore) Expire(key string, timeout time.Duration) error

Expire set value expire time.

func (*MemoryStore) Flush

func (s *MemoryStore) Flush() error

Remove all items from the cache.

func (*MemoryStore) Forever added in v1.2.0

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

Forever Store an item in the cache indefinitely.

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) Increment added in v1.0.1

func (s *MemoryStore) Increment(key string, value ...int) (int, error)

Increment the value of an item in the cache.

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.

func (*MemoryStore) TTL added in v1.0.1

func (s *MemoryStore) TTL(key string) (int64, error)

func (*MemoryStore) Tags added in v1.2.0

func (s *MemoryStore) Tags(names ...string) Store

type RedisStore

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

func NewRedisStore

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

NewRedisStore Create a redis cache store

func (*RedisStore) Decrement added in v1.0.1

func (s *RedisStore) Decrement(key string, value ...int) (int, error)

Decrement the value of an item in the cache.

func (*RedisStore) Exist

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

Exist check cache's existence in redis.

func (*RedisStore) Expire added in v1.0.1

func (s *RedisStore) Expire(key string, timeout time.Duration) error

Expire set value expire time.

func (*RedisStore) Flush

func (s *RedisStore) Flush() error

Remove all items from the cache.

func (*RedisStore) FlushByPrefix added in v1.2.1

func (s *RedisStore) FlushByPrefix(prefix string) error

func (*RedisStore) Forever added in v1.2.0

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

Forever Store an item in the cache indefinitely.

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) Increment added in v1.0.1

func (s *RedisStore) Increment(key string, value ...int) (int, error)

Increment the value of an item in the cache.

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.

func (*RedisStore) TTL added in v1.0.1

func (s *RedisStore) TTL(key string) (int64, error)

func (*RedisStore) Tags added in v1.2.0

func (s *RedisStore) Tags(names ...string) Store

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) Decrement added in v1.0.1

func (r *Repository) Decrement(key string, value ...int) (int, error)

Decrement the value of an item in the cache.

func (*Repository) Delete

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

Delete Alias for the "Delete" method.

func (*Repository) Expire added in v1.0.1

func (r *Repository) Expire(key string, timeout time.Duration) error

Expire set value expire time.

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) Increment added in v1.0.1

func (r *Repository) Increment(key string, value ...int) (int, error)

Increment the value of an item 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.

func (*Repository) TTL added in v1.0.1

func (r *Repository) TTL(key string) (int64, error)

TTL get the ttl of the key.

func (*Repository) Tags added in v1.2.0

func (r *Repository) Tags(names ...string) *Repository

Tags Begin executing a new tags operation if the store supports it.

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

	// Increment the value of an item in the cache.
	Increment(key string, value ...int) (int, error)

	// Decrement the value of an item in the cache.
	Decrement(key string, value ...int) (int, error)

	// Forever Store an item in the cache indefinitely.
	Forever(key string, val interface{}) error

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

	// Expire set value expire time.
	Expire(key string, timeout time.Duration) error

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

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

	Tags(names ...string) Store

	// TTL get the ttl of the key.
	TTL(key string) (int64, error)
}

type TagSet added in v1.2.0

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

func NewTagSet added in v1.2.0

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

func (*TagSet) GetNamespace added in v1.2.0

func (t *TagSet) GetNamespace() (string, error)

func (*TagSet) Reset added in v1.2.0

func (t *TagSet) Reset() error

func (*TagSet) ResetTag added in v1.2.0

func (t *TagSet) ResetTag(name string) (string, error)

func (*TagSet) TagId added in v1.2.0

func (t *TagSet) TagId(name string) (string, error)

func (*TagSet) TagKey added in v1.2.0

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

Jump to

Keyboard shortcuts

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