cachemdl

package
v2.0.0-beta.1 Latest Latest
Warning

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

Go to latest
Published: Dec 28, 2020 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package cachemdl will help cache object into memory. It Uses LRU algo

Index

Constants

View Source
const (
	// TypeFastCache indicates fast cache as cache storage
	TypeFastCache = iota + 1
	// TypeRedisCache indicates redis server as cache storage. Use this in grid mode.
	TypeRedisCache
)

Variables

This section is empty.

Functions

func FCWithCleanupInterval

func FCWithCleanupInterval(ivl time.Duration) fastCacheOption

func FCWithExpiration

func FCWithExpiration(exp time.Duration) fastCacheOption

func FCWithMaxEntries

func FCWithMaxEntries(i int) fastCacheOption

func RedisWithAddr

func RedisWithAddr(addr string) redisOption

func RedisWithDB

func RedisWithDB(db int) redisOption

func RedisWithExpiration

func RedisWithExpiration(exp time.Duration) redisOption

func RedisWithPassword

func RedisWithPassword(p string) redisOption

func RedisWithPrefix

func RedisWithPrefix(pfx string) redisOption

Types

type Cacher

type Cacher interface {
	// SET
	Set(key string, val interface{})
	SetWithExpiration(key string, val interface{}, exp time.Duration)
	SetNoExpiration(key string, val interface{})

	// GET
	Get(key string) (interface{}, bool)
	GetAll() map[string]interface{}

	// DELETE
	Delete(key string)
	Purge()

	// GetItemsCount
	GetItemsCount() int

	Type() int
}

Cacher provides access to underlying cache, make sure all caches implement these methods.

The return types of data can be different. Ex. In case of redis cache it is `string`. The caller needs to handle this with the help of Type() method.

Ex. (Cacher{}).Type() == TypeRedisCache { // your implementation }

type FastCacheHelper

type FastCacheHelper struct {
	FastCache   *cache.Cache
	Expiration  time.Duration
	CleanupTime time.Duration

	MaxEntries int
}

FastCacheHelper -

func SetupFastCache

func SetupFastCache(opts ...fastCacheOption) *FastCacheHelper

SetupFastCache initializes fastcache cache for application and returns its instance.

func (*FastCacheHelper) Delete

func (fastCacheHelper *FastCacheHelper) Delete(key string)

Delete -

func (*FastCacheHelper) Get

func (fastCacheHelper *FastCacheHelper) Get(key string) (interface{}, bool)

Get -

func (*FastCacheHelper) GetAll

func (fastCacheHelper *FastCacheHelper) GetAll() map[string]interface{}

GetAll returns all keys with values present in memory. **This is not intended for production use. May hamper performance**

func (*FastCacheHelper) GetItems

func (fastCacheHelper *FastCacheHelper) GetItems() map[string]cache.Item

GetItems -

func (*FastCacheHelper) GetItemsCount

func (fastCacheHelper *FastCacheHelper) GetItemsCount() int

GetItemsCount : Number of items in the cache

func (*FastCacheHelper) Purge

func (fastCacheHelper *FastCacheHelper) Purge()

Purge -

func (*FastCacheHelper) Set

func (fastCacheHelper *FastCacheHelper) Set(key string, object interface{})

Set -

func (*FastCacheHelper) SetNoExpiration

func (fastCacheHelper *FastCacheHelper) SetNoExpiration(key string, object interface{})

SetNoExpiration -

func (*FastCacheHelper) SetWithExpiration

func (fastCacheHelper *FastCacheHelper) SetWithExpiration(key string, object interface{}, duration time.Duration)

SetWithExpiration -

func (*FastCacheHelper) Setup

func (fastCacheHelper *FastCacheHelper) Setup(maxEntries int, expiration time.Duration, cleanupTime time.Duration)

Setup initializes fastcache cache for application. Must be called only once.

func (*FastCacheHelper) Type

func (fh *FastCacheHelper) Type() int

type RedisCache

type RedisCache struct {
	Addr       string        // redis server address, default "127.0.0.1:6379"
	DB         int           // redis DB on provided server, default 0
	Password   string        //
	Expiration time.Duration // this duration will be used for Set() method
	Prefix     string        // this will be used for storing keys for provided project
	// contains filtered or unexported fields
}

RedisCache represents a Redis client with provided configuration. Do not change configuration at runtime.

func SetupRedisCache

func SetupRedisCache(opts ...redisOption) (*RedisCache, error)

SetupRedisCache initializes redis cache for application and returns it. Must be called only once.

func (*RedisCache) Delete

func (rc *RedisCache) Delete(key string)

Delete -

func (*RedisCache) Get

func (rc *RedisCache) Get(key string) (interface{}, bool)

Get returns data against provided key. Returns false if not present.

func (*RedisCache) GetAll

func (rc *RedisCache) GetAll() map[string]interface{}

GetAll returns all keys with values present in redis server. Excludes the keys which does not have specified prefix. If prefix is empty, then returns all keys.

**This is not intended for production use. May hamper performance**

func (*RedisCache) GetItemsCount

func (rc *RedisCache) GetItemsCount() int

GetItemsCount -

func (*RedisCache) Purge

func (rc *RedisCache) Purge()

Purge deletes for current redis db

func (*RedisCache) Set

func (rc *RedisCache) Set(key string, val interface{})

Set marshalls provided value and stores against provided key. Errors will be logged to initialized logger.

func (*RedisCache) SetNoExpiration

func (rc *RedisCache) SetNoExpiration(key string, val interface{})

SetNoExpiration marshalls provided value and stores against provided key. Errors will be logged to initialized logger.

func (*RedisCache) SetWithExpiration

func (rc *RedisCache) SetWithExpiration(key string, val interface{}, exp time.Duration)

SetWithExpiration marshalls provided value and stores against provided key for given duration. Errors will be logged to initialized logger.

func (*RedisCache) Setup

func (rc *RedisCache) Setup(addr, password, prefix string, db int, exp time.Duration)

Setup initializes redis cache for application. Must be called only once.

func (*RedisCache) Type

func (rc *RedisCache) Type() int

Jump to

Keyboard shortcuts

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