cache

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jun 8, 2024 License: CC0-1.0 Imports: 3 Imported by: 0

README

cache

cache abstraction for Golang

Supported cache

  • In Memory
  • Redis
  • LRU
  • Embed (Badger)

Quick Start

Installation $ go get github.com/diki-haryadi/govega/cache

Usage

Interface:

type Cache interface {
	Set(ctx context.Context, key string, value interface{}, expiration int) error
	Increment(ctx context.Context, key string, expiration int) (int64, error)
	Get(ctx context.Context, key string) ([]byte, error)
	GetObject(ctx context.Context, key string, doc interface{}) error
	GetString(ctx context.Context, key string) (string, error)
	GetInt(ctx context.Context, key string) (int64, error)
	GetFloat(ctx context.Context, key string) (float64, error)
	Exist(ctx context.Context, key string) bool
	Delete(ctx context.Context, key string) error
	RemainingTime(ctx context.Context, key string) int
	Close() error
}

Example:

package main

import (
	cache "github.com/diki-haryadi/govega/cache"
	_ "github.com/diki-haryadi/govega/cache/mem"
	_ "github.com/diki-haryadi/govega/cache/redis"
	_ "github.com/diki-haryadi/govega/cache/lru"
	_ "github.com/diki-haryadi/govega/cache/embed"
)

func main() {
	// Use in-memory store
	memcache, _ := cache.New("mem://")
	rediscache, _ := cache.New("redis://<user>:<pass>@localhost:6379/prefix")
	lru, _ := cache.New("lru://local/1024")
	embed, _ := cache.New("embed://tmp/mydb")
}

Documentation

Index

Constants

View Source
const NotFound = CacheError("[cache] not found")
View Source
const NotSupported = CacheError("[cache] not supported")

Variables

This section is empty.

Functions

func Register

func Register(schema string, f InitFunc)

Register register cache implementation

Types

type Cache

type Cache interface {
	Set(ctx context.Context, key string, value interface{}, expiration int) error
	Increment(ctx context.Context, key string, expiration int) (int64, error)
	Get(ctx context.Context, key string) ([]byte, error)
	GetObject(ctx context.Context, key string, doc interface{}) error
	GetString(ctx context.Context, key string) (string, error)
	GetInt(ctx context.Context, key string) (int64, error)
	GetFloat(ctx context.Context, key string) (float64, error)
	Exist(ctx context.Context, key string) bool
	Delete(ctx context.Context, key string, opts ...DeleteOptions) error
	GetKeys(ctx context.Context, pattern string) []string
	RemainingTime(ctx context.Context, key string) int
	Close() error
}

Cache cache service

func New

func New(urlStr string) (Cache, error)

New create new cache

type CacheError

type CacheError string

func (CacheError) Error

func (e CacheError) Error() string

type DeleteCache

type DeleteCache struct {
	Pattern string
}

type DeleteOptions

type DeleteOptions func(options *DeleteCache)

type InitFunc

type InitFunc func(url *url.URL) (Cache, error)

InitFunc cache init function

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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