nscache

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Nov 18, 2022 License: Apache-2.0 Imports: 7 Imported by: 4

README

nscache

Build License Go Reference Go Report Card codecov Release

Installation

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

Quick Start

Current support following cache

  • memory memory:
  • redis redis://127.0.0.1:6379
  • buntdb buntdb://:memory: or buntdb://buntdb.db
  • etcd etcd://127.0.0.1:2379?dial_timeout=5s

For example, init a memory cache, write and read data.

package main

import (
	"time"

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

func main() {
	c, err := nscache.NewCache("memory:")
	if err != nil {
		log.Error(err, "init cache error")
		return
	}
	k := "hello"
	c.Set(k, "world", time.Minute)
	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)
}

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
}

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

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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