cachestore

package module
v0.17.0 Latest Latest
Warning

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

Go to latest
Published: Dec 31, 2023 License: MIT Imports: 15 Imported by: 1

README

Cache Store Open in Gitpod

Tests Status Go Report Card PkgGoDev

Cache messages to a database table.

Installation

go get -u github.com/gouniverse/cachestore

Setup

cacheStore = cachestore.NewStore(NewStoreOptions{
	DB:                 db,
	CacheTableName:     "my_cache",
	AutomigrateEnabled: false,
	DebugEnabled: false,
})

go cacheStore.ExpireCacheGoroutine()

Usage

  • Set value to cache with expiration
isSaved, err := cacheStore.Set("token", "ABCDEFGHIJKLMNOPQRSTVUXYZ", 60*60) // 1 hour (= 60 min * 60 sec)
if isSaved == false {
	log.Println("Saving failed")
	return
}
  • Get value from cache with default if not found
token, err := cacheStore.Get("token", "") // "" - default value, if the key has expired, or missing
  • Set and retrieve complex value as JSON
isSaved, err := cacheStore.Set("token", map[string]string{"first_name": "Jo"}, 60*60) // 1 hour (= 60 min * 60 sec)
if isSaved == false {
	log.Println("Saving failed")
	return
}

value, err := store.GetJSON("hello", "")

if err != nil {
	log.Fatalf("Getting JSON failed:" + err.Error())
}

result := value.(map[string]interface{})

log.Println(result["first_name"])

Changelog

2022.12.17 - Changed setup for new store

2021.12.31 - Fixed GetJSON and added tests

2021.12.29 - Cache ID updated to nano precission

2021.12.27 - Cache key length increased

2021.12.12 - Added license

2021.12.12 - Added tests badge

2021.12.12 - Fixed bug where DB scanner was returning empty values

2021.12.09 - Added support for DB dialects

2021.09.11 - Removed GORM dependency and moved to the standard library

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

type Cache struct {
	ID        string     `db:"id"`
	Key       string     `db:"cache_key"`
	Value     string     `db:"cache_value"`
	ExpiresAt *time.Time `db:"expires_at"`
	CreatedAt time.Time  `db:"created_at"`
	UpdatedAt time.Time  `db:"updated_at"`
	DeletedAt *time.Time `db:"deleted_at"`
}

Cache type

type NewStoreOptions added in v0.15.0

type NewStoreOptions struct {
	CacheTableName     string
	DB                 *sql.DB
	DbDriverName       string
	TimeoutSeconds     int64
	AutomigrateEnabled bool
	DebugEnabled       bool
}

NewStoreOptions define the options for creating a new session store

type Store

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

Store defines a session store

func NewStore

func NewStore(opts NewStoreOptions) (*Store, error)

NewStore creates a new entity store

func (*Store) AutoMigrate

func (st *Store) AutoMigrate() error

AutoMigrate auto migrate

func (*Store) DriverName

func (st *Store) DriverName(db *sql.DB) string

DriverName finds the driver name from database

func (*Store) EnableDebug

func (st *Store) EnableDebug(debugEnabled bool)

EnableDebug - enables the debug option

func (*Store) ExpireCacheGoroutine

func (st *Store) ExpireCacheGoroutine() error

ExpireCacheGoroutine - soft deletes expired cache

func (*Store) FindByKey

func (st *Store) FindByKey(key string) (*Cache, error)

FindByKey finds a cache by key

func (*Store) Get

func (st *Store) Get(key string, valueDefault string) (string, error)

Get gets a key from cache

func (*Store) GetJSON

func (st *Store) GetJSON(key string, valueDefault interface{}) (interface{}, error)

GetJSON gets a JSON key from cache

func (*Store) Remove

func (st *Store) Remove(key string) error

Remove removes a key from cache

func (*Store) SQLCreateTable

func (st *Store) SQLCreateTable() string

SQLCreateTable returns a SQL string for creating the cache table

func (*Store) Set

func (st *Store) Set(key string, value string, seconds int64) error

Set sets new key value pair

func (*Store) SetJSON

func (st *Store) SetJSON(key string, value interface{}, seconds int64) error

SetJSON sets new key JSON value pair

Jump to

Keyboard shortcuts

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