https://github.com/metafates/gache
🗃️ Dead simple caching library for go
https://github.com/metafates/gache
Last synced: over 1 year ago
JSON representation
🗃️ Dead simple caching library for go
- Host: GitHub
- URL: https://github.com/metafates/gache
- Owner: metafates
- License: mit
- Created: 2022-11-01T11:11:56.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2022-11-01T18:43:36.000Z (over 3 years ago)
- Last Synced: 2025-02-01T06:12:44.091Z (over 1 year ago)
- Language: Go
- Homepage: https://pkg.go.dev/github.com/metafates/gache
- Size: 19.5 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Gache
Gache is a dead simple file-based (or in-memory) cache library for Go with zero dependencies.
There are great caching libraries out there, but none of them fit my needs or not as simple as I'd like to.
So I decided to write my own and share it with the world. 🐳
## Installation
```bash
go get github.com/metafates/gache
```
## Docs
[Godoc](https://pkg.go.dev/github.com/metafates/gache)
## Usage Example
```go
package main
import (
"encoding/json"
"fmt"
"github.com/metafates/gache"
"net/http"
"time"
)
type Pokemon struct {
Height int
}
// Create new cache instance
var cache = gache.New[map[string]*Pokemon](&gache.Options{
// Path to cache file
// If not set, cache will be in-memory
Path: ".cache/pokemons.json",
// Lifetime of cache.
// If not set, cache will never expire
Lifetime: time.Hour,
})
// getPokemon will get a pokemon by name from API
// Gonna Cache Em' All!
func getPokemon(name string) (*Pokemon, error) {
// check if Pokémon is in cache
pokemons, expired, err := cache.Get()
if err != nil {
return nil, err
}
if pokemons == nil {
pokemons = make(map[string]*Pokemon)
}
// if cache is expired, or Pokémon wasn't cached
// Fetch it from API
if pokemon, ok := pokemons[name]; !expired && ok {
return pokemon, nil
}
// bla-bla-bla, boring stuff, etc...
resp, err := http.Get("https://pokeapi.co/api/v2/pokemon/" + name)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var pokemon Pokemon
if err := json.NewDecoder(resp.Body).Decode(&pokemon); err != nil {
return nil, err
}
// okay, we got our Pokémon, let's cache it
pokemons[name] = &pokemon
_ = cache.Set(pokemons)
return &pokemon, nil
}
func main() {
start := time.Now()
for i := 0; i < 3; i++ {
_, _ = getPokemon("pikachu")
}
fmt.Println(time.Since(start))
}
```