mirror of
https://github.com/gookit/cache.git
synced 2025-09-26 20:21:16 +08:00
196 lines
4.3 KiB
Go
196 lines
4.3 KiB
Go
// Package cache is a generic cache use and cache manager for golang.
|
|
// FileCache is a simple local file system cache implement.
|
|
// MemoryCache is a simple memory cache implement.
|
|
package cache
|
|
|
|
import (
|
|
"time"
|
|
|
|
"github.com/gookit/gsr"
|
|
)
|
|
|
|
// Cache interface definition
|
|
type Cache = gsr.SimpleCacher
|
|
|
|
// some generic expire time define.
|
|
const (
|
|
// Forever Always exist
|
|
Forever = 0
|
|
|
|
// Seconds1 1 second
|
|
Seconds1 = time.Second
|
|
// Seconds2 2 second
|
|
Seconds2 = 2 * time.Second
|
|
// Seconds3 3 second
|
|
Seconds3 = 3 * time.Second
|
|
// Seconds5 5 second
|
|
Seconds5 = 5 * time.Second
|
|
// Seconds6 6 second
|
|
Seconds6 = 6 * time.Second
|
|
// Seconds7 7 second
|
|
Seconds7 = 7 * time.Second
|
|
// Seconds8 8 second
|
|
Seconds8 = 8 * time.Second
|
|
// Seconds9 9 second
|
|
Seconds9 = 9 * time.Second
|
|
// Seconds10 10 second
|
|
Seconds10 = 10 * time.Second
|
|
// Seconds15 15 second
|
|
Seconds15 = 15 * time.Second
|
|
// Seconds20 20 second
|
|
Seconds20 = 20 * time.Second
|
|
// Seconds30 30 second
|
|
Seconds30 = 30 * time.Second
|
|
|
|
// OneMinutes 1 minutes
|
|
OneMinutes = 60 * time.Second
|
|
// TwoMinutes 2 minutes
|
|
TwoMinutes = 120 * time.Second
|
|
// ThreeMinutes 3 minutes
|
|
ThreeMinutes = 180 * time.Second
|
|
// FiveMinutes 5 minutes
|
|
FiveMinutes = 300 * time.Second
|
|
// TenMinutes 10 minutes
|
|
TenMinutes = 600 * time.Second
|
|
// FifteenMinutes 15 minutes
|
|
FifteenMinutes = 900 * time.Second
|
|
// HalfHour half an hour
|
|
HalfHour = 1800 * time.Second
|
|
// OneHour 1 hour
|
|
OneHour = 3600 * time.Second
|
|
// TwoHour 2 hours
|
|
TwoHour = 7200 * time.Second
|
|
// ThreeHour 3 hours
|
|
ThreeHour = 10800 * time.Second
|
|
// HalfDay 12 hours(half of the day)
|
|
HalfDay = 43200 * time.Second
|
|
// OneDay 24 hours(1 day)
|
|
OneDay = 86400 * time.Second
|
|
// TwoDay 2 day
|
|
TwoDay = 172800 * time.Second
|
|
// ThreeDay 3 day
|
|
ThreeDay = 259200 * time.Second
|
|
// OneWeek 7 day(one week)
|
|
OneWeek = 604800 * time.Second
|
|
)
|
|
|
|
/*************************************************************
|
|
* config default cache manager
|
|
*************************************************************/
|
|
|
|
// default cache driver manager instance
|
|
var std = NewManager()
|
|
|
|
// Register driver to manager instance
|
|
func Register(name string, driver Cache) *Manager {
|
|
std.Register(name, driver)
|
|
return std
|
|
}
|
|
|
|
// Unregister an cache driver
|
|
func Unregister(name string) int {
|
|
return std.Unregister(name)
|
|
}
|
|
|
|
// UnregisterAll cache drivers
|
|
func UnregisterAll(fn ...func(cache Cache)) int {
|
|
return std.UnregisterAll(fn...)
|
|
}
|
|
|
|
// SetDefName set default driver name.
|
|
// Deprecated
|
|
//
|
|
// please use DefaultUse() instead it
|
|
func SetDefName(driverName string) {
|
|
std.DefaultUse(driverName)
|
|
}
|
|
|
|
// DefaultUse set default driver name
|
|
func DefaultUse(driverName string) {
|
|
std.DefaultUse(driverName)
|
|
}
|
|
|
|
// Use driver object by name and set it as default driver.
|
|
func Use(driverName string) Cache {
|
|
return std.Use(driverName)
|
|
}
|
|
|
|
// GetCache returns a driver instance by name. alias of Driver()
|
|
func GetCache(driverName string) Cache {
|
|
return std.Cache(driverName)
|
|
}
|
|
|
|
// Driver get a driver instance by name
|
|
func Driver(driverName string) Cache {
|
|
return std.Driver(driverName)
|
|
}
|
|
|
|
// Std get default cache manager instance
|
|
func Std() *Manager {
|
|
return std
|
|
}
|
|
|
|
// DefManager get default cache manager instance
|
|
func DefManager() *Manager {
|
|
return std
|
|
}
|
|
|
|
// Default get default cache driver instance
|
|
func Default() Cache {
|
|
return std.Default()
|
|
}
|
|
|
|
// Close all drivers
|
|
func Close() error {
|
|
return std.Close()
|
|
}
|
|
|
|
// ClearAll all drivers caches
|
|
func ClearAll() error {
|
|
return std.ClearAll()
|
|
}
|
|
|
|
/*************************************************************
|
|
* quick use by default cache driver
|
|
*************************************************************/
|
|
|
|
// Has cache key
|
|
func Has(key string) bool {
|
|
return std.Default().Has(key)
|
|
}
|
|
|
|
// Get value by key
|
|
func Get(key string) any {
|
|
return std.Default().Get(key)
|
|
}
|
|
|
|
// Set value by key
|
|
func Set(key string, val any, ttl time.Duration) error {
|
|
return std.Default().Set(key, val, ttl)
|
|
}
|
|
|
|
// Del value by key
|
|
func Del(key string) error {
|
|
return std.Default().Del(key)
|
|
}
|
|
|
|
// GetMulti values by keys
|
|
func GetMulti(keys []string) map[string]any {
|
|
return std.Default().GetMulti(keys)
|
|
}
|
|
|
|
// SetMulti values
|
|
func SetMulti(mv map[string]any, ttl time.Duration) error {
|
|
return std.Default().SetMulti(mv, ttl)
|
|
}
|
|
|
|
// DelMulti values by keys
|
|
func DelMulti(keys []string) error {
|
|
return std.Default().DelMulti(keys)
|
|
}
|
|
|
|
// Clear all caches
|
|
func Clear() error {
|
|
return std.Default().Clear()
|
|
}
|