cache

package
v1.2.1 Latest Latest
Warning

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

Go to latest
Published: Apr 6, 2024 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DefaultExpiryTime  = time.Duration(0)
	ForEverNeverExpiry = time.Duration(-1)
)

Length of time to cache an item.

Variables

View Source
var (
	Instance Cache

	ErrCacheMiss    = errors.New("revel/cache: key not found")
	ErrNotStored    = errors.New("revel/cache: not stored")
	ErrInvalidValue = errors.New("revel/cache: invalid value")
)

Functions

func Add

func Add(key string, value interface{}, expires time.Duration) error

func Decrement

func Decrement(key string, n uint64) (newValue uint64, err error)

func Delete

func Delete(key string) error

func Deserialize

func Deserialize(byt []byte, ptr interface{}) (err error)

Deserialize transforms bytes produced by Serialize back into a Go object, storing it into "ptr", which must be a pointer to the value type.

func Flush

func Flush() error

func Get

func Get(key string, ptrValue interface{}) error

func Increment

func Increment(key string, n uint64) (newValue uint64, err error)

func Replace

func Replace(key string, value interface{}, expires time.Duration) error

func Serialize

func Serialize(value interface{}) ([]byte, error)

Serialize transforms the given value into bytes following these rules:

  • If value is a byte array, it is returned as-is.
  • If value is an int or uint type, it is returned as the ASCII representation
  • Else, encoding/gob is used to serialize

func Set

func Set(key string, value interface{}, expires time.Duration) error

Types

type Cache

type Cache interface {
	// The Cache implements a Getter.
	Getter

	// Set the given key/value in the cache, overwriting any existing value
	// associated with that key.  Keys may be at most 250 bytes in length.
	//
	// Returns:
	//   - nil on success
	//   - an implementation specific error otherwise
	Set(key string, value interface{}, expires time.Duration) error

	// Get the content associated multiple keys at once.  On success, the caller
	// may decode the values one at a time from the returned Getter.
	//
	// Returns:
	//   - the value getter, and a nil error if the operation completed.
	//   - an implementation specific error otherwise
	GetMulti(keys ...string) (Getter, error)

	// Delete the given key from the cache.
	//
	// Returns:
	//   - nil on a successful delete
	//   - ErrCacheMiss if the value was not in the cache
	//   - an implementation specific error otherwise
	Delete(key string) error

	// Add the given key/value to the cache ONLY IF the key does not already exist.
	//
	// Returns:
	//   - nil if the value was added to the cache
	//   - ErrNotStored if the key was already present in the cache
	//   - an implementation-specific error otherwise
	Add(key string, value interface{}, expires time.Duration) error

	// Set the given key/value in the cache ONLY IF the key already exists.
	//
	// Returns:
	//   - nil if the value was replaced
	//   - ErrNotStored if the key does not exist in the cache
	//   - an implementation specific error otherwise
	Replace(key string, value interface{}, expires time.Duration) error

	// Increment the value stored at the given key by the given amount.
	// The value silently wraps around upon exceeding the uint64 range.
	//
	// Returns the new counter value if the operation was successful, or:
	//   - ErrCacheMiss if the key was not found in the cache
	//   - an implementation specific error otherwise
	Increment(key string, n uint64) (newValue uint64, err error)

	// Decrement the value stored at the given key by the given amount.
	// The value is capped at 0 on underflow, with no error returned.
	//
	// Returns the new counter value if the operation was successful, or:
	//   - ErrCacheMiss if the key was not found in the cache
	//   - an implementation specific error otherwise
	Decrement(key string, n uint64) (newValue uint64, err error)

	// Expire all cache entries immediately.
	// This is not implemented for the memcached cache (intentionally).
	// Returns an implementation specific error if the operation failed.
	Flush() error
}

Cache is an interface to an expiring cache. It behaves (and is modeled) like the Memcached interface. It is keyed by strings (250 bytes at most).

Many callers will make exclusive use of Set and Get, but more exotic functions are also available.

Example

Here is a typical Get/Set interaction:

var items []*Item
if err := cache.Get("items", &items); err != nil {
  items = loadItems()
  go cache.Set("items", items, cache.DefaultExpiryTime)
}

Note that the caller will frequently not wait for Set() to complete.

Errors

It is assumed that callers will infrequently check returned errors, since any request should be fulfillable without finding anything in the cache. As a result, all errors other than ErrCacheMiss and ErrNotStored will be logged to revel.ERROR, so that the developer does not need to check the return value to discover things like deserialization or connection errors.

type Getter

type Getter interface {
	// Get the content associated with the given key. decoding it into the given
	// pointer.
	//
	// Returns:
	//   - nil if the value was successfully retrieved and ptrValue set
	//   - ErrCacheMiss if the value was not in the cache
	//   - an implementation specific error otherwise
	Get(key string, ptrValue interface{}) error
}

Getter is an interface for getting / decoding an element from a cache.

func GetMulti

func GetMulti(keys ...string) (Getter, error)

type InMemoryCache

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

func NewInMemoryCache

func NewInMemoryCache(defaultExpiration time.Duration) InMemoryCache

func (InMemoryCache) Add

func (c InMemoryCache) Add(key string, value interface{}, expires time.Duration) error

func (InMemoryCache) Decrement

func (c InMemoryCache) Decrement(key string, n uint64) (newValue uint64, err error)

func (InMemoryCache) Delete

func (c InMemoryCache) Delete(key string) error

func (InMemoryCache) Flush

func (c InMemoryCache) Flush() error

func (InMemoryCache) Get

func (c InMemoryCache) Get(key string, ptrValue interface{}) error

func (InMemoryCache) GetMulti

func (c InMemoryCache) GetMulti(keys ...string) (Getter, error)

func (InMemoryCache) Increment

func (c InMemoryCache) Increment(key string, n uint64) (newValue uint64, err error)

func (InMemoryCache) Replace

func (c InMemoryCache) Replace(key string, value interface{}, expires time.Duration) error

func (InMemoryCache) Set

func (c InMemoryCache) Set(key string, value interface{}, expires time.Duration) error

type ItemMapGetter

type ItemMapGetter map[string]*memcache.Item

ItemMapGetter implements a Getter on top of the returned item map.

func (ItemMapGetter) Get

func (g ItemMapGetter) Get(key string, ptrValue interface{}) error

type MemcachedCache

type MemcachedCache struct {
	*memcache.Client
	// contains filtered or unexported fields
}

MemcachedCache wraps the Memcached client to meet the Cache interface.

func NewMemcachedCache

func NewMemcachedCache(hostList []string, defaultExpiration time.Duration) MemcachedCache

func (MemcachedCache) Add

func (c MemcachedCache) Add(key string, value interface{}, expires time.Duration) error

func (MemcachedCache) Decrement

func (c MemcachedCache) Decrement(key string, delta uint64) (newValue uint64, err error)

func (MemcachedCache) Delete

func (c MemcachedCache) Delete(key string) error

func (MemcachedCache) Flush

func (c MemcachedCache) Flush() error

func (MemcachedCache) Get

func (c MemcachedCache) Get(key string, ptrValue interface{}) error

func (MemcachedCache) GetMulti

func (c MemcachedCache) GetMulti(keys ...string) (Getter, error)

func (MemcachedCache) Increment

func (c MemcachedCache) Increment(key string, delta uint64) (newValue uint64, err error)

func (MemcachedCache) Replace

func (c MemcachedCache) Replace(key string, value interface{}, expires time.Duration) error

func (MemcachedCache) Set

func (c MemcachedCache) Set(key string, value interface{}, expires time.Duration) error

type RedisCache

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

RedisCache wraps the Redis client to meet the Cache interface.

func NewRedisCache

func NewRedisCache(host string, password string, defaultExpiration time.Duration) RedisCache

NewRedisCache returns a new RedisCache with given parameters until redigo supports sharding/clustering, only one host will be in hostList.

func (RedisCache) Add

func (c RedisCache) Add(key string, value interface{}, expires time.Duration) error

func (RedisCache) Decrement

func (c RedisCache) Decrement(key string, delta uint64) (newValue uint64, err error)

func (RedisCache) Delete

func (c RedisCache) Delete(key string) error

func (RedisCache) Flush

func (c RedisCache) Flush() error

func (RedisCache) Get

func (c RedisCache) Get(key string, ptrValue interface{}) error

func (RedisCache) GetMulti

func (c RedisCache) GetMulti(keys ...string) (Getter, error)

func (RedisCache) Increment

func (c RedisCache) Increment(key string, delta uint64) (uint64, error)

func (RedisCache) Replace

func (c RedisCache) Replace(key string, value interface{}, expires time.Duration) error

func (RedisCache) Set

func (c RedisCache) Set(key string, value interface{}, expires time.Duration) error

type RedisItemMapGetter

type RedisItemMapGetter map[string][]byte

RedisItemMapGetter implements a Getter on top of the returned item map.

func (RedisItemMapGetter) Get

func (g RedisItemMapGetter) Get(key string, ptrValue interface{}) error

Jump to

Keyboard shortcuts

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