cccache

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: May 12, 2023 License: Apache-2.0 Imports: 12 Imported by: 0

README

cc-go-cache

Installation

go get github.com/crosect/cc-go-cache

Configuration

app.cache:
  driver: "memory" #support memory, redis
  # If use memory
  memory:
    defaultExpiration: "30s" # 30s, 30m, 30h 
    cleanupInterval: "1s" # 1s, 1m, 1h
  # If use redis
  redis:
    host: localhost
    port: 6379
    database: 0
    user: username
    password: secret
    enableTLS: true #default: false

Usage

Register to fx container

package bootstrap

import (
	"github.com/crosect/cc-go-cache"
	"go.uber.org/fx"
)

func Register() fx.Option {
	return cccache.EnableCache()
}

Remember function will get value in the cache if exists, if not exists, it will set to cache

package app

import (
	"github.com/crosect/cc-go-cache"
	"time"
)

type NeedCache struct {
	cache *cccache.Cache
}

func (nc *NeedCache) UseRemember() {
	// String
	str, err := nc.cache.Remember("key", 30*time.Second, func() (interface{}, error) {
		return "value", nil
	})
	// Number
	num, err := nc.cache.Remember("key", 30*time.Second, func() (interface{}, error) {
		return 100, nil
	})
	// Struct
	value, err := nc.cache.Remember("key", 30*time.Second, func() (interface{}, error) {
		return &Example{Data: "data"}, nil
	})
	data := value.(*Example).Data
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func EnableCache

func EnableCache() fx.Option

func NewMemoryStore

func NewMemoryStore(properties MemoryCacheProperties) (store.StoreInterface, error)

func NewRedisStore

func NewRedisStore(properties RedisCacheProperties) (store.StoreInterface, error)

func NewStore

func NewStore(properties *CacheProperties) (store.StoreInterface, error)

Types

type Cache

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

func NewCache

func NewCache(properties *CacheProperties) (*Cache, error)

func (*Cache) AsyncSet

func (c *Cache) AsyncSet(key string, value interface{}, duration time.Duration)

func (*Cache) Clear

func (c *Cache) Clear() error

func (*Cache) Delete

func (c *Cache) Delete(key string) error

func (*Cache) Exist

func (c *Cache) Exist(key string) bool

func (*Cache) Get

func (c *Cache) Get(key string) (interface{}, error)

func (*Cache) Remember

func (c *Cache) Remember(key string, duration time.Duration, fn func() (interface{}, error)) (interface{}, error)

func (*Cache) Set

func (c *Cache) Set(key string, value interface{}, duration time.Duration) error

type CacheProperties

type CacheProperties struct {
	Driver string
	Memory MemoryCacheProperties
	Redis  RedisCacheProperties
}

CacheProperties represents ...

func NewCacheProperties

func NewCacheProperties(loader config.Loader) (*CacheProperties, error)

NewCacheProperties return a new CacheProperties instance

func (*CacheProperties) Prefix

func (t *CacheProperties) Prefix() string

Prefix return config prefix

type MemoryCacheProperties

type MemoryCacheProperties struct {
	DefaultExpiration time.Duration `default:"30s"`
	CleanupInterval   time.Duration `default:"30s"`
}

MemoryCacheProperties represent memory cache properties

type RedisCacheProperties

type RedisCacheProperties struct {
	Host      string
	Port      int
	Database  int
	User      string
	Password  string
	EnableTLS bool
}

RedisCacheProperties represents redis cache properties

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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