Go asynchronous LRU cache
A lru is an asynchronous LRU cache (generic version).
For example, caching the output of the ioutil.ReadFile function to reduce disk I/O.
package main
import (
"log"
"os"
"github.com/codeation/lru"
)
func readFileContent(key string) ([]byte, error) {
log.Println("read once")
return os.ReadFile(key)
}
func main() {
cache := lru.NewSyncLRU(1024, readFileContent)
for i := 0; i < 10; i++ {
data, err := cache.Get("input.txt")
if err != nil {
log.Fatal(err)
}
log.Printf("file size is %d\n", len(data))
}
}
The lru.NewSyncLRU parameter is the number of cache items until the last used item is removed from the cache. The second parameter is a func to get the value for the specified key and error.
The parameter of cache.Get func is a key (filename in this case). An error is returned when the function returns an error.