README

Gache

GoDoc Build Status Maintainability Test Coverage

A simple cache interface for Go applications.

Overview

A cache instance can be configured to store items in memory or in Redis. Use the NewMemoryCache or NewRedisCache to create an instance of the cache.

// Store a value in the cache with the tag `products`
if err := cache.SetValue("product-123", "...", "products"); err != nil {
    // ...
}

// Retrieve the value by key from the cache
val, err := cache.GetValue("product-123")
if err != nil {
    // ...
}

// Process the value if it exists
if val != "" {
    // ...
}

// Remove all keys with the tag `products` after a change to the
// data which backs that set of cache keys.
if err := cache.BustTags("products"); err != nil {
    // ...
}

License

Copyright (c) 2018 Eric Fritz

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

type Cache interface {
	// GetValue retrieves a value associated with a key from
	// the backend store. If the key does not exist, an empty
	// string should be returned.
	GetValue(key string) (string, error)

	// SetValue associates a key with a value in the backend
	// store. Any additional tags supplied should be attached
	// to the key. If the key already exists, the value should
	// be updated. Concrete implementations of this interface
	// are not required to clear old tags from the key (thus,
	// clients should keep tags associated with a key stable).
	SetValue(key, value string, tags ...string) error

	// Remove removes a value from the backend store.
	Remove(key string) error

	// BustTags removes all keys associated with the given tags
	// from the backend store.
	BustTags(tags ...string) error
}

Cache is an interface to a backend key/value map with additional bookkeeping for attaching tas to certain keys.

func NewMemoryCache

func NewMemoryCache(configs ...MemoryConfig) Cache

NewMemoryCache creates a Cache instance using a local memory backend.

func NewRedisCache

func NewRedisCache(client deepjoy.Client, configs ...RedisConfig) Cache

NewRedisCache creates a Cache instance using a local memory backend.

type MemoryConfig

type MemoryConfig func(*memoryCache)

MemoryConfig is a function use dot configure instances of a Cache which uses a memory backend.

func WithMemoryCapacity

func WithMemoryCapacity(capacity int) MemoryConfig

WithMemoryCapacity sets the maximum number of items which are able to be stored in the memory backend.

type RedisConfig

type RedisConfig func(*redisCache)

RedisConfig is a function use dot configure instances of a Cache which uses a Redis backend.

func WithRedisPrefix

func WithRedisPrefix(prefix string) RedisConfig

WithRedisPrefix sets the prefix used in the Redis backend.

func WithRedisTTL

func WithRedisTTL(ttl time.Duration) RedisConfig

WithRedisTTL sets the TTL for keys stored in the Redis backend.

Directories

Path Synopsis