cachestore

package module
v0.23.0 Latest Latest
Warning

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

Go to latest
Published: Nov 8, 2025 License: MIT Imports: 11 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/dracory/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

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 StoreInterface

type StoreInterface interface {
	AutoMigrate() error
	EnableDebug(debugEnabled bool)
	DriverName(db *sql.DB) string

	ExpireCacheGoroutine(ctx context.Context) error

	Set(key string, value string, seconds int64) error
	Get(key string, valueDefault string) (string, error)
	SetJSON(key string, value any, seconds int64) error
	GetJSON(key string, valueDefault any) (any, error)
	Remove(key string) error
	FindByKey(key string) (*Cache, error)
}

func NewStore

func NewStore(opts NewStoreOptions) (StoreInterface, error)

NewStore creates a new entity store

Jump to

Keyboard shortcuts

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