Documentation ¶
Overview ¶
Example (InMemory) ¶
package main import ( "context" "errors" "fmt" "time" "github.com/pace/bricks/pkg/cache" ) func main() { ctx := context.Background() // init cache var c cache.Cache = cache.InMemory() // write to cache if err := c.Put(ctx, "foo", []byte("bar"), time.Hour); err != nil { panic(err) } // get from cache and print v, _, err := c.Get(ctx, "foo") if err != nil { panic(err) } fmt.Println(string(v)) // forget if err := c.Forget(ctx, "foo"); err != nil { panic(err) } // get from cache and print _, _, err = c.Get(ctx, "foo") if errors.Is(err, cache.ErrNotFound) { fmt.Println(err) } else { panic("expected error not found") } }
Output: bar key "foo": not found
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( // The value under the given key was not found. ErrNotFound = errors.New("not found") // The caching backend produced an error that is not reflected by any other // error. ErrBackend = errors.New("cache backend error") )
Package errors.
Functions ¶
This section is empty.
Types ¶
type Cache ¶
type Cache interface { // Put stores the value under the key. Any existing value is overwritten. If // ttl is given, the cache automatically forgets the value after the // duration. If ttl is zero then it is never automatically forgotten. Put(ctx context.Context, key string, value []byte, ttl time.Duration) error // Get returns the value stored under the key and its remaining ttl. If // there is no value stored, ErrNotFound is returned. If the ttl is zero, // the value does not automatically expire. Unless an error is returned, the // value is always non-nil. Get(ctx context.Context, key string) (value []byte, ttl time.Duration, _ error) // Forget removes the value stored under the key. No error is returned if // there is no value stored. Forget(ctx context.Context, key string) error }
Cache is a common abstraction to cache bytes. It is safe for concurrent use.
type Memory ¶
type Memory struct {
// contains filtered or unexported fields
}
Memory is the cache that stores everything in memory. It is safe for concurrent use.
func (*Memory) Forget ¶
Forget removes the value stored under the key. No error is returned if there is no value stored.
func (*Memory) Get ¶
Get returns the value stored under the key and its remaining ttl. If there is no value stored, ErrNotFound is returned. If the ttl is zero, the value does not automatically expire. Unless an error is returned, the value is always non-nil.
type Redis ¶
type Redis struct {
// contains filtered or unexported fields
}
Redis is the cache that uses a redis backend. It is safe for concurrent use.
func InRedis ¶
InRedis returns a new cache that connects to redis using the given client. The prefix is used for every key that is stored.
func (*Redis) Forget ¶
Forget removes the value stored under the key. No error is returned if there is no value stored.
func (*Redis) Get ¶
Get returns the value stored under the key and its remaining ttl. If there is no value stored, ErrNotFound is returned. If the ttl is zero, the value does not automatically expire. Unless an error is returned, the value is always non-nil.