nscache

package module
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Jul 2, 2025 License: Apache-2.0 Imports: 7 Imported by: 4

README

nscache

Build License Go Reference Go Report Card codecov Release Mentioned in Awesome Go

Installation

go get -u github.com/no-src/nscache

Quick Start

First, you need to import the cache driver, then create your cache component instance with the specified connection string and use it.

Current support following cache drivers

Driver Import Driver Package Connection String Example
Memory github.com/no-src/nscache/memory memory:
Redis github.com/no-src/nscache/redis redis://127.0.0.1:6379
Redis Cluster github.com/no-src/nscache/redis_cluster redis-cluster://127.0.0.1:7001?addr=127.0.0.1:7002&addr=127.0.0.1:7003
BuntDB github.com/no-src/nscache/buntdb buntdb://:memory: or buntdb://buntdb.db
Etcd github.com/no-src/nscache/etcd etcd://127.0.0.1:2379?dial_timeout=5s
BoltDB github.com/no-src/nscache/boltdb boltdb://boltdb.db
FreeCache github.com/no-src/nscache/freecache freecache://?cache_size=50mib
BigCache github.com/no-src/nscache/bigcache bigcache://?eviction=10m
FastCache github.com/no-src/nscache/fastcache fastcache://?max_bytes=50mib
Memcached github.com/no-src/nscache/memcached memcached://127.0.0.1:11211
Proxy github.com/no-src/nscache/proxy/client proxy://127.0.0.1:8080

For example, initial a memory cache and write, read and remove data.

package main

import (
	"time"

	_ "github.com/no-src/nscache/memory"

	"github.com/no-src/log"
	"github.com/no-src/nscache"
)

func main() {
	// initial cache driver
	c, err := nscache.NewCache("memory:")
	if err != nil {
		log.Error(err, "init cache error")
		return
	}
	defer c.Close()

	// write data
	k := "hello"
	c.Set(k, "world", time.Minute)

	// read data
	var v string
	if err = c.Get(k, &v); err != nil {
		log.Error(err, "get cache error")
		return
	}
	log.Info("key=%s value=%s", k, v)

	// remove data
	if err = c.Remove(k); err != nil {
		log.Error(err, "remove cache error")
		return
	}
}

Command Line Tool

You can use the command line tool to operate all the cache drivers that nscache supports.

Install the command line tool nscache-cli.

go install github.com/no-src/nscache/cmd/nscache-cli@latest

Run the cli tool with specified cache driver connection string to operate cache.

nscache-cli memory:

Proxy

Proxy Server

Install the proxy server nscache-server.

go install github.com/no-src/nscache/cmd/nscache-server@latest

Run the proxy server with specified listen address and cache driver connection string.

nscache-server -addr=:8080 -conn=memory:
Proxy Client

You can use the code to operate cache proxy server like the example code in the Quick Start section.

Or use the nscache-cli to connect to the proxy server and operate cache.

nscache-cli proxy://127.0.0.1:8080

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrNil = errors.New("nscache: nil")

ErrNil get nil data

Functions

func Register

func Register(name string, factory CacheFactoryFunc) (overwritten bool)

Register register a new cache driver

Types

type CacheFactoryFunc

type CacheFactoryFunc func(conn *url.URL) (NSCache, error)

CacheFactoryFunc the cache driver factory function

type NSCache

type NSCache interface {
	NSCacheExt

	// Get get cache data by key
	Get(k string, v any) error

	// Set set new cache data
	Set(k string, v any, expiration time.Duration) error

	// Remove remove the specified key
	Remove(k string) error

	// Close close the cache component
	Close() error
}

NSCache the core interface of the cache

func NewCache

func NewCache(conn string) (NSCache, error)

NewCache get an instance of NSCache by connection string

type NSCacheExt

type NSCacheExt interface {
	// GetBool get bool cache data by key
	GetBool(k string) (v bool, ok bool)
	// GetUint8 get uint8 cache data by key
	GetUint8(k string) (v uint8, ok bool)
	// GetUint16 get uint16 cache data by key
	GetUint16(k string) (v uint16, ok bool)
	// GetUint32 get uint32 cache data by key
	GetUint32(k string) (v uint32, ok bool)
	// GetUint64 get uint64 cache data by key
	GetUint64(k string) (v uint64, ok bool)
	// GetInt8 get int8 cache data by key
	GetInt8(k string) (v int8, ok bool)
	// GetInt16 get int16 cache data by key
	GetInt16(k string) (v int16, ok bool)
	// GetInt32 get int32 cache data by key
	GetInt32(k string) (v int32, ok bool)
	// GetInt64 get int64 cache data by key
	GetInt64(k string) (v int64, ok bool)
	// GetFloat32 get float32 cache data by key
	GetFloat32(k string) (v float32, ok bool)
	// GetFloat64 get float64 cache data by key
	GetFloat64(k string) (v float64, ok bool)
	// GetString get string cache data by key
	GetString(k string) (v string, ok bool)
	// GetStrings get string list cache data by key
	GetStrings(k string) (v []string, ok bool)
	// GetInt get int cache data by key
	GetInt(k string) (v int, ok bool)
	// GetInts get int list cache data by key
	GetInts(k string) (v []int, ok bool)
	// GetUint get uint cache data by key
	GetUint(k string) (v uint, ok bool)
	// GetUintptr get uintptr cache data by key
	GetUintptr(k string) (v uintptr, ok bool)
	// GetByte get byte cache data by key
	GetByte(k string) (v byte, ok bool)
	// GetBytes get byte list cache data by key
	GetBytes(k string) (v []byte, ok bool)
	// GetRune get rune cache data by key
	GetRune(k string) (v rune, ok bool)
	// GetTime get time.Time cache data by key
	GetTime(k string) (v time.Time, ok bool)
	// GetDuration get time.Duration cache data by key
	GetDuration(k string) (v time.Duration, ok bool)
}

NSCacheExt the NSCache extension function collection

Jump to

Keyboard shortcuts

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