cache

package
v0.0.0-...-73fb834 Latest Latest
Warning

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

Go to latest
Published: Sep 20, 2019 License: GPL-3.0 Imports: 6 Imported by: 0

README

Caching

A base class package for caching.

Constructing

You can construct the cache like so:

// main.go
package main

import (
    // ...
    "github.com/vigilantapps/viper-common/cache"
)

func main() {
    //...

    // connecting to a cache:
    // if envvar DISABLE_CACHE=1, then a null cache is returned
    // otherwise the predicate argument is called and your new cache
    // is returned
	c := cache.Bootstrap(func() (cache.Abstraction, error) {
        return cache.Redis(os.Getenv("CACHE_ADDR"), 0, expirationWindow)
    })
    
    // ...
}

Usage / Examples

The recommendation is that you build a higher-order component which wraps this cache instead of exposing it directly. Keys should be sensible and unique, JSON for serialising values; examples of which can be found here:

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Abstraction

type Abstraction interface {
	Set(key string, val string) error
	Get(key string) (string, error)
}

Abstraction represents the abstract concept of a cache. Anything that implements these methods can be used as a cache.

func Bootstrap

func Bootstrap(whenEnabled func() (Abstraction, error)) Abstraction

Bootstrap should be called in your main() to create a cache instance. It will look and if envvar DISABLE_CACHE=1, then it will return a null implementation of a cache.

func None

func None() Abstraction

None creates a new instance of a null implementation cache

func Redis

func Redis(addr string, db int, expr time.Duration) (Abstraction, error)

Redis creates a new RedisCache instance with the chosen database and connection string.

type RedisClient

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

RedisClient represents an active connection to the cache.

func (RedisClient) Get

func (c RedisClient) Get(key string) (string, error)

Get an item to the cache if present.

func (RedisClient) Set

func (c RedisClient) Set(key string, val string) error

Set adds an item to the cache with the default expiry duration.

Jump to

Keyboard shortcuts

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