Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/muesli/cache2go
Concurrency-safe Go caching library with expiration capabilities and access counters
https://github.com/muesli/cache2go
hacktoberfest
Last synced: 2 days ago
JSON representation
Concurrency-safe Go caching library with expiration capabilities and access counters
- Host: GitHub
- URL: https://github.com/muesli/cache2go
- Owner: muesli
- License: other
- Created: 2013-11-11T03:45:02.000Z (about 11 years ago)
- Default Branch: master
- Last Pushed: 2024-07-02T15:31:52.000Z (5 months ago)
- Last Synced: 2024-11-26T05:02:59.605Z (16 days ago)
- Topics: hacktoberfest
- Language: Go
- Homepage:
- Size: 116 KB
- Stars: 2,128
- Watchers: 67
- Forks: 517
- Open Issues: 35
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE.txt
Awesome Lists containing this project
- awesome-go - cache2go - In-memory key:value cache which supports automatic invalidation based on timeouts. (Database / Caches)
- my-awesome - muesli/cache2go - 07 star:2.1k fork:0.5k Concurrency-safe Go caching library with expiration capabilities and access counters (Go)
- awesome-go-storage - cache2go - An in-memory key:value cache which supports automatic invalidation based on timeouts. (Database)
- go-awesome - cache2go
- jimsghstars - muesli/cache2go - Concurrency-safe Go caching library with expiration capabilities and access counters (Go)
- awesome-go - cache2go - In-memory key:value cache which supports automatic invalidation based on timeouts. Stars:`2.1K`. (Database / Caches)
- awesome-go - cache2go - Concurrency-safe Go caching library with expiration capabilities and access counters - ★ 684 (Database)
- awesome-go-extra - cache2go - safe Go caching library with expiration capabilities and access counters|1808|495|27|2013-11-11T03:45:02Z|2022-07-23T08:46:45Z| (Generators / Caches)
- awesome-go-storage - cache2go - An in-memory key:value cache which supports automatic invalidation based on timeouts. (Database)
README
# cache2go
[![Latest Release](https://img.shields.io/github/release/muesli/cache2go.svg)](https://github.com/muesli/cache2go/releases)
[![Build Status](https://github.com/muesli/cache2go/workflows/build/badge.svg)](https://github.com/muesli/cache2go/actions)
[![Coverage Status](https://coveralls.io/repos/github/muesli/cache2go/badge.svg?branch=master)](https://coveralls.io/github/muesli/cache2go?branch=master)
[![Go ReportCard](https://goreportcard.com/badge/muesli/cache2go)](https://goreportcard.com/report/muesli/cache2go)
[![GoDoc](https://godoc.org/github.com/golang/gddo?status.svg)](https://pkg.go.dev/github.com/muesli/cache2go)Concurrency-safe golang caching library with expiration capabilities.
## Installation
Make sure you have a working Go environment (Go 1.2 or higher is required).
See the [install instructions](https://golang.org/doc/install.html).To install cache2go, simply run:
go get github.com/muesli/cache2go
To compile it from source:
cd $GOPATH/src/github.com/muesli/cache2go
go get -u -v
go build && go test -v## Example
```go
package mainimport (
"github.com/muesli/cache2go"
"fmt"
"time"
)// Keys & values in cache2go can be of arbitrary types, e.g. a struct.
type myStruct struct {
text string
moreData []byte
}func main() {
// Accessing a new cache table for the first time will create it.
cache := cache2go.Cache("myCache")// We will put a new item in the cache. It will expire after
// not being accessed via Value(key) for more than 5 seconds.
val := myStruct{"This is a test!", []byte{}}
cache.Add("someKey", 5*time.Second, &val)// Let's retrieve the item from the cache.
res, err := cache.Value("someKey")
if err == nil {
fmt.Println("Found value in cache:", res.Data().(*myStruct).text)
} else {
fmt.Println("Error retrieving value from cache:", err)
}// Wait for the item to expire in cache.
time.Sleep(6 * time.Second)
res, err = cache.Value("someKey")
if err != nil {
fmt.Println("Item is not cached (anymore).")
}// Add another item that never expires.
cache.Add("someKey", 0, &val)// cache2go supports a few handy callbacks and loading mechanisms.
cache.SetAboutToDeleteItemCallback(func(e *cache2go.CacheItem) {
fmt.Println("Deleting:", e.Key(), e.Data().(*myStruct).text, e.CreatedOn())
})// Remove the item from the cache.
cache.Delete("someKey")// And wipe the entire cache table.
cache.Flush()
}
```To run this example, go to examples/mycachedapp/ and run:
go run mycachedapp.go
You can find a [few more examples here](https://github.com/muesli/cache2go/tree/master/examples).
Also see our test-cases in cache_test.go for further working examples.