caching

package
v0.0.12 Latest Latest
Warning

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

Go to latest
Published: Apr 25, 2025 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Overview

Package caching provides an easy abstraction layer for adding caching to an application

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

type Cache interface {
	//SetKey sets a key value pair. It prefixes the key provided
	//with the specified prefix in the cacheConfig.
	//in case the ttl is not specified it uses the default ttl specified in the config.
	//if the ttl is not specified in the config. Then the expiration is at midnight
	SetKey(key string, value string, ttl *time.Duration) error
	SetKeyIndex(route string, key string) error
	DeleteCacheIndex(indexKey string) (int, error)
	Get(key string) ([]byte, error)
	CacheIncrement(key string, expiration time.Duration) error
	Delete(keys ...string) (int, error)
}

Cache All methods required to be implemented for a technology to be considered a cache

func GetCache

func GetCache(cacheType string, c *CacheConfig) Cache

GetCache is a factory that returns a caching layer for all methods that are commonly used for adding a caching layer to your API. gives flexibility when building and deploying the API. it allows for easy benchmarks and swapping technologies it is also easier for development to use redis for example in order to access the cache data with an app like RedisInsight

func SetUpRedisDB

func SetUpRedisDB(c *CacheConfig) Cache

SetUpRedisDB initializes the redis instance and makes a ping to check the connection

type CacheConfig added in v0.0.8

type CacheConfig struct {
	//Addr is the cache DSN.
	// memcached default: "127.0.0.1:11211"
	// redis default: "127.0.0.1:6379"
	Addr string `json:"addr" xml:"addr" yaml:"addr" csv:"addr"`
	// Enabled caching should be enabled across the application
	Enabled *bool `json:"enabled" xml:"enabled" yaml:"enabled" csv:"enabled"`
	// Prefix is primarily used for namespacing your application.
	// ie. "myProject:api"
	Prefix string `json:"prefix" xml:"prefix" yaml:"prefix" csv:"prefix"`
	// HashKeys hashes the keys befora adding them.
	// it avoids issues related to long key names & hides the parameters used
	HashKeys *bool `json:"hash_keys" xml:"hash_keys" yaml:"hash_keys" csv:"hash_keys"`
	//DefaultExpiration specifies a default value for the expiration date of a key.
	//The default value is today at midnight.
	DefaultExpiration *time.Duration `json:"default_expiration" xml:"default_expiration" yaml:"default_expiration" csv:"default_expiration"`
}

type MemcachedDB

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

MemcachedDB provides a struct for the memcache implementation of caching

func SetUpMemcachedDB

func SetUpMemcachedDB(opts *CacheConfig) *MemcachedDB

SetUpMemcachedDB initializes the memcache connection

func (*MemcachedDB) CacheIncrement

func (m *MemcachedDB) CacheIncrement(key string, expiration time.Duration) error

func (*MemcachedDB) Delete added in v0.0.9

func (m *MemcachedDB) Delete(indexKeys ...string) (int, error)

func (*MemcachedDB) DeleteCacheIndex

func (m *MemcachedDB) DeleteCacheIndex(indexKey string) (int, error)

DeleteCacheIndex clears the cache indexes for a provided route

func (*MemcachedDB) Get

func (m *MemcachedDB) Get(key string) ([]byte, error)

Get returns bytes from memcache

func (*MemcachedDB) SetKey

func (m *MemcachedDB) SetKey(key string, value string, ttl *time.Duration) error

SetKey easier way to set a cache key Default expiration date is today at midnight

func (*MemcachedDB) SetKeyIndex

func (m *MemcachedDB) SetKeyIndex(indexKey string, member string) error

SetKeyIndex appends to the list of keys A list of the cached keys is maintained in the cache with no expiration so when it comes to invalidating routes with dynamic filters you know all the cached keys

type RedisDB

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

RedisDB is the redis client implementation of cache

func (*RedisDB) CacheIncrement

func (r *RedisDB) CacheIncrement(key string, expiration time.Duration) error

func (*RedisDB) Delete added in v0.0.9

func (r *RedisDB) Delete(indexKeys ...string) (int, error)

func (*RedisDB) DeleteCacheIndex

func (r *RedisDB) DeleteCacheIndex(indexKey string) (int, error)

DeleteCacheIndex clears the cache indexes for a provided route

func (*RedisDB) Get

func (r *RedisDB) Get(key string) ([]byte, error)

Get gets the value of a key from redis

func (*RedisDB) SetKey

func (r *RedisDB) SetKey(key string, value string, ttl *time.Duration) error

SetKey sets a string type in redis with a provided value and a ttl.

func (*RedisDB) SetKeyIndex

func (r *RedisDB) SetKeyIndex(indexKey, member string) error

SetKeyIndex sets indexes for a key. Uses the underline set in redis

Jump to

Keyboard shortcuts

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