gocacher

package module
v0.0.0-...-9bac901 Latest Latest
Warning

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

Go to latest
Published: Apr 11, 2016 License: MIT Imports: 9 Imported by: 0

README

Gocacher

Build Status Coverage Status

Gocacher is cache abstraction. It's intended to use in web applications with possibility to choose cache implementation directly from configuration (dsn).

Currently redis implementation is written and tested, but in the future there will be more implementations (memcached, and other..).

All cache implementations satisfy this interface

type Cacher interface {
	// returns cache value by key
	Get(key string) ([]byte, error)

	// Sets key to value, if expiration is not given it's used from settings
	Set(key string, value []byte, expiration ...time.Duration) error

	// Deletes key in cache
	Delete(key string) error

	// Increments key by 1, if num is given by that amout will be incremented
	Incr(key string, num ...int) (int, error)

	// Decrements key by 1, if num is given it decrements by given number
	Decr(key string, num ...int) (int, error)

	// Return cache to cache pool
	Close() error
}

Before we dive into examples all use this import

import (
	"time"
	"github.com/phonkee/gocacher"
)

Examples:

// Open Cache with expiration of 60 seconds and prefix "cache"
cache, _ := gocacher.Open("redis://localhost:5379/1?expiration=60&prefix=cache")

// this key will be set with expiration set in url "expiration=60"
cache.Set("key", []byte("message"))

// value will be []byte("message")
value, _ := cache.Get("key")

// this key will expire in 5 seconds
cache.Set("key-with-expiration", []byte("message"), time.Seconds*5)

// deletes the key from cache
cache.Delete("key")

// increments key and returns incremented value
i, _ := cache.Incr("increment-key") // i is now 1
i, _ := cache.Incr("increment-key", 10) // i is now 11

i, _ := cache.Decr("increment-key", 5) // i is now 6
i, _ := cache.Decr("increment-key") // i is now 5

Open Cache connection

You can open connection in two ways.

cache, _ := gocacher.Open("redis://localhost:5379/1")

or you can provide connection to gocacher.OpenConnection and provide additional settings as url query

pool := &redis.Pool{
	Dial: func() (redis.Conn, error) {
		return redis.Dial("tcp", "localhost:5379")
	},
}

cache, _ := gocacher.OpenConnection("redis", pool)

// With additional cache settings
cache, _ := gocacher.OpenConnection("redis", pool, "expiration=60&prefix=cache")
Parameters

You can pass multiple parameters to Open or OpenConnection as query part of dsn.

e.g.

cache, _ := gocacher.Open("locmem:///0?expiration=10s")
cache, _ := gocacher.Open("redis://localhost:5379/1?expiration=60&prefix=cache")
Cache implementations

Currently two imlpementations are available:

  • redis - redis storage
  • locmem - local memory storage
Locmem

Local memory cache. Supports multiple databases. Currently there is no garbage collect or limiting of items in database. This will be updated in near future.

cache, _ := gocacher.Open("locmem:///0")
parameters:
  • expiration - string parsed with time parse duration. (default expiration is 0s - neverending)
Redis

Redis cache support. Supports multiple databases and all commands.

parameters:
  • pool_max_active - redis pool settings
  • pool_max_idle - redis pool settings
  • pool_idle_timeout - redis pool settings
  • expiration - default expiration
  • prefix - prefix for cache keys

Contribute

Welcome!

Documentation

Index

Constants

View Source
const (
	URL_PARAM_POOL_MAX_ACTIVE   = "pool_max_active"
	URL_PARAM_POOL_MAX_IDLE     = "pool_max_idle"
	URL_PARAM_POOL_IDLE_TIMEOUT = "pool_idle_timeout"
	URL_PARAM_EXPIRATION        = "expiration"
	URL_PARAM_PREFIX            = "prefix"

	DEFAULT_POOL_MAX_ACTIVE   = 20
	DEFAULT_POOL_MAX_IDLE     = 10
	DEFAULT_POOL_IDLE_TIMEOUT = 200 * time.Millisecond
	DEFAULT_EXPIRATION        = 0 * time.Second
	DEFAULT_PREFIX            = ""
)
View Source
const (
	LOCMEM_DEFAULT_DATABASE = "default"
)

Variables

View Source
var (
	ErrNotFound = errors.New("locmem item not found")
)

Functions

func Drivers

func Drivers() []string

returns list of available driver names

func Register

func Register(name string, driver CacheDriverer)

Register makes a cache driver available by the provided name. If Register is called twice with the same name or if driver is nil, it panics.

Types

type CacheDriverer

type CacheDriverer interface {
	Open(dsn string) (Cacher, error)

	OpenConnection(connection interface{}, settings ...string) (Cacher, error)
}

CacheDriverer interface

All cache drivers must satisfy this interface.

type Cacher

type Cacher interface {
	// returns cache value by key
	Get(key string) ([]byte, error)

	// Sets key to value, if expiration is not given it's used from settings
	Set(key string, value []byte, expiration ...time.Duration) error

	// Deletes key in cache
	Delete(key string) error

	// Increments key by 1, if num is given by that amout will be incremented
	Incr(key string, num ...int) (int, error)

	// Decrements key by 1, if num is given it decrements by given number
	Decr(key string, num ...int) (int, error)

	// Return cache to cache pool
	Close() error
}

Cache interface

All cache implementations must satisfy this interface.

func Open

func Open(dsn string) (Cacher, error)

Opens cache by dsn This method is preferred to use for instantiate Cache. DSN is used as connector. Additional settings are set as url parameters.

Example:

redis://localhost:5379/0?pool_max_active=10&expiration=10

Which translates to connection to localhost:5379 redis pool max_active=10 and expiration for cache.Set default value will be set to 10 seconds

func OpenConnection

func OpenConnection(driver string, connection interface{}, settings ...string) (Cacher, error)

opens message queue by name and connection

type PoolSettings

type PoolSettings struct {
	MaxIdle     int
	MaxActive   int
	IdleTimeout time.Duration
}

type RedisCache

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

Redis cache implementation

func (*RedisCache) Close

func (r *RedisCache) Close() error

closes cache (it's underlying connection)

func (*RedisCache) Decr

func (r *RedisCache) Decr(key string, num ...int) (int, error)

Decrements key in cache

func (*RedisCache) Delete

func (r *RedisCache) Delete(key string) error

deletes from cache

func (*RedisCache) Get

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

returns data from cache

func (*RedisCache) Incr

func (r *RedisCache) Incr(key string, num ...int) (int, error)

Increments key in cache

func (*RedisCache) Set

func (r *RedisCache) Set(key string, value []byte, expiration ...time.Duration) error

Sets to cache

type RedisDSN

type RedisDSN struct {
	*godsn.DSN
	// contains filtered or unexported fields
}

redis dsn

func ParseRedisDSN

func ParseRedisDSN(dsn string) (*RedisDSN, error)

func (*RedisDSN) Database

func (r *RedisDSN) Database() int

func (*RedisDSN) Password

func (r *RedisDSN) Password() string

func (*RedisDSN) Pool

func (r *RedisDSN) Pool() *redis.Pool

returns *redis.Poool

type RedisDriver

type RedisDriver struct{}

func (*RedisDriver) Open

func (r *RedisDriver) Open(dsn string) (Cacher, error)

func (*RedisDriver) OpenConnection

func (r *RedisDriver) OpenConnection(connection interface{}, settings ...string) (Cacher, error)

type RedisSettings

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

func NewRedisSettings

func NewRedisSettings(values *godsn.DSNValues) (*RedisSettings, error)

func NewRedisSettingsFromQuery

func NewRedisSettingsFromQuery(query string) (*RedisSettings, error)

func (*RedisSettings) Prefixed

func (r *RedisSettings) Prefixed(str string) string

returns prefixed string

Jump to

Keyboard shortcuts

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