Documentation
¶
Index ¶
- Constants
- Variables
- func Id(params ...string) string
- func IsErrorOk(err error) bool
- func TypeAssert(source, target interface{}) (err error)
- type Cache
- func File() (Cache, error)
- func Memory() Cache
- func NewFileCache(dir string, ttl, tickerTtl time.Duration) (Cache, error)
- func NewMemoryCache(ttl, tickerTtl time.Duration) Cache
- func NewRedisCache(ttl time.Duration, poolSize int, prefix, addr string) (Cache, error)
- func NewSqlCache(ttl, tickerTtl time.Duration, sql *sql.DB, tableName string, ...) (Cache, error)
- func Redis(addr string) (Cache, error)
- func Sql(driverName, dataSourceName string) (Cache, error)
Examples ¶
Constants ¶
View Source
const FileIndex = "github.com/gadelkareem/cachita/file-index"
Variables ¶
View Source
var ( ErrNotFound = errors.New("cachita: cache not found") ErrExpired = errors.New("cachita: cache expired") )
Functions ¶
func TypeAssert ¶
func TypeAssert(source, target interface{}) (err error)
Types ¶
type Cache ¶
type Cache interface {
Get(key string, i interface{}) error
Put(key string, i interface{}, ttl time.Duration) error // ttl 0:default ttl, -1: keep forever
Incr(key string, ttl time.Duration) (int64, error)
Tag(key string, tags ...string) error
Exists(key string) bool
Invalidate(key string) error
InvalidateMulti(keys ...string) error
InvalidateTags(tags ...string) error
}
Example ¶
package main
import (
"fmt"
"time"
"github.com/gadelkareem/cachita"
)
func main() {
cache := cachita.Memory()
err := cache.Put("cache_key", "some data", 1*time.Minute)
if err != nil {
panic(err)
}
if cache.Exists("cache_key") {
// do something
}
var holder string
err = cache.Get("cache_key", &holder)
if err != nil && err != cachita.ErrNotFound {
panic(err)
}
fmt.Printf("%s", holder) // prints "some data"
err = cache.Invalidate("cache_key")
if err != nil {
panic(err)
}
}
Output: some data
func File ¶
Example ¶
package main
import (
"fmt"
"time"
"github.com/gadelkareem/cachita"
)
func main() {
cache, err := cachita.File()
if err != nil {
panic(err)
}
err = cache.Put("cache_key", "some data", 1*time.Minute)
if err != nil {
panic(err)
}
var holder string
err = cache.Get("cache_key", &holder)
if err != nil && err != cachita.ErrNotFound {
panic(err)
}
fmt.Printf("%s", holder) // prints "some data"
}
Output: some data
func Memory ¶
func Memory() Cache
Example ¶
package main
import (
"fmt"
"net/url"
"github.com/gadelkareem/cachita"
)
func main() {
var u url.URL
cacheId := cachita.Id(u.Scheme, u.Host, u.RequestURI())
obj := make(map[string]interface{})
obj["test"] = "data"
err := cachita.Memory().Put(cacheId, obj, 0)
if err != nil {
panic(err)
}
var cacheObj map[string]interface{}
err = cachita.Memory().Get(cacheId, &cacheObj)
if err != nil && err != cachita.ErrNotFound && err != cachita.ErrExpired {
panic(err)
}
fmt.Printf("%+v", cacheObj)
}
Output: map[test:data]
func NewMemoryCache ¶
Example ¶
package main
import (
"fmt"
"time"
"github.com/gadelkareem/cachita"
)
func main() {
cache := cachita.NewMemoryCache(1*time.Millisecond, 1*time.Minute) // default ttl 1 millisecond
err := cache.Put("cache_key", "some data", 0) // ttl = 0 means use default
if err != nil {
panic(err)
}
time.Sleep(2 * time.Millisecond)
fmt.Printf("%t", cache.Exists("cache_key"))
}
Output: false
func NewRedisCache ¶
func NewSqlCache ¶
Click to show internal directories.
Click to hide internal directories.