simplecache

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Mar 9, 2021 License: MIT Imports: 3 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

simplecache

Simple cache abstraction for Golang

Supported cache

  • In Memory
  • Redis
  • LRU

Quick Start

Installation $ go get github.com/akulnurislam/simplecache

Usage

Interface:

type Cache interface {
	Set(ctx context.Context, key string, value interface{}, expiration int) 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/akulnurislam/simplecache"
	_ "github.com/akulnurislam/simplecache/mem"
	_ "github.com/akulnurislam/simplecache/redis"
	_ "github.com/akulnurislam/simplecache/lru"
)

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")
}

Documentation

Index

Constants

This section is empty.

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
	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
}

Cache cache service

func New

func New(urlStr string) (Cache, error)

New create new cache

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