go get -d github.com/morkid/gocache
package main
import (
"time"
"fmt"
"github.com/morkid/gocache"
)
func main() {
config := gocache.InMemoryCacheConfig{
ExpiresIn: 10 * time.Second,
}
adapter := *gocache.NewInMemoryCache(config)
adapter.Set("foo", "bar")
if adapter.IsValid("foo") {
value, err := adapter.Get("foo")
if nil != err {
fmt.Println(err.Error())
} else if value != "bar" {
fmt.Println("value not equals to bar")
}
adapter.Clear("foo")
}
}
package main
import (
"os"
"time"
"fmt"
"github.com/morkid/gocache"
)
func main() {
config := gocache.DiskCacheConfig{
Directory: os.TempDir(),
ExpiresIn: 10 * time.Second,
}
adapter := *gocache.NewDiskCache(config)
adapter.Set("foo", "bar")
if adapter.IsValid("foo") {
value, err := adapter.Get("foo")
if nil != err {
fmt.Println(err.Error())
} else if value != "bar" {
fmt.Println("value not equals to bar")
}
adapter.Clear("foo")
}
}
You can create your custom cache adapter by implementing the AdapterInterface
:
type AdapterInterface interface {
Set(key string, value string) error
Get(key string) (string, error)
IsValid(key string) bool
Clear(key string) error
ClearPrefix(keyPrefix string) error
ClearAll() error
}
Published under the MIT License.