Documentation ¶
Overview ¶
Package cache provides a cache manager for any type that implements the cache.Interface.
Example ¶
This example demonstrate the basics of the cache interface. For more details check the documentation.
package main import ( "fmt" "time" cm "github.com/patrickascher/gofw/cache" ) func main() { // import the provider package // import _ "github.com/patrickascher/gofw/cache/memory" // Initialize cache. Each call is creating a new cache instance. // The gc will be spawned in the background. c, err := cm.New(cm.MEMORY, nil) if err != nil { return } // Set a cache item for 5 hours. err = c.Set("foo", "bar", 5*time.Hour) // Set a cache item infinity err = c.Set("John", "Doe", cm.INFINITY) // Get a cache by key. item, err := c.Get("foo") if err != nil && item != nil { item.Value() // value of the item "bar" } // Get all items as map items := c.GetAll() fmt.Println(items) // Check if an key exists exists := c.Exist("foo") fmt.Println(exists) // Delete by key err = c.Delete("foo") // Delete all items err = c.DeleteAll() }
Output:
Index ¶
Examples ¶
Constants ¶
View Source
const ( // MEMORY defined cache provider. MEMORY = "memory" // INFINITY should be used by the cache providers to identify // that the value should not get deleted by the garbage collector. INFINITY = 0 )
Variables ¶
View Source
var ( ErrUnknownProvider = errors.New("cache: unknown cache-provider %q") ErrNoProvider = errors.New("cache: empty cache-name or cache-provider is nil") ErrProviderAlreadyExists = errors.New("cache: cache-provider %#v is already registered") )
Error messages
Functions ¶
Types ¶
type Interface ¶
type Interface interface { // Get returns a Valuer by its key. // If it does not exist, an error will return. Get(key string) (Valuer, error) // GetAll returns all existing Valuer as map. GetAll() map[string]Valuer // GetAll returns all existing Valuer as map. GetPrefixed(string) map[string]Valuer // Set a value by its key and lifetime. // If a value should not get deleted, cache.INFINITY can be used as time.Duration. Set(key string, value interface{}, ttl time.Duration) error // Exist checks if a value is set by the given key. Exist(key string) bool // Delete a value by its key. Delete(key string) error // DeleteAll values of the cache provider. DeleteAll() error DeletePrefixed(string) error // GC will spawn the garbage collector in a goroutine. // If your cache provider has its own gc (redis, memcached, ...) just return void in this method. GC() }
Interface is used by cache providers.
Click to show internal directories.
Click to hide internal directories.