Skip to content

Commit

Permalink
add docs
Browse files Browse the repository at this point in the history
  • Loading branch information
umputun committed Jan 24, 2019
1 parent 958afc0 commit a4ad3fa
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 6 deletions.
18 changes: 14 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
# Loading Cache Wrapper [![Build Status](https://travis-ci.org/go-pkgz/lcw.svg?branch=master)](https://travis-ci.org/go-pkgz/lcw) [![Coverage Status](https://coveralls.io/repos/github/go-pkgz/lcw/badge.svg?branch=master)](https://coveralls.io/github/go-pkgz/lcw?branch=master) [![godoc](https://godoc.org/github.com/go-pkgz/lcw?status.svg)](https://godoc.org/github.com/go-pkgz/lcw)


The library adds a thin layer on top of [lru cache](https://github.com/hashicorp/golang-lru).
The library adds a thin layer on top of [lru cache](https://github.com/hashicorp/golang-lru) and [patrickmn/go-cache](https://github.com/patrickmn/go-cache).

It exposes 3 caches, implementing `LoadingCache` interface:

1. `LruCache` - LRU cache with limits, created with `lcw.NewLruCache`
1. `ExpirableCache` - TTL cache with limits, created with `lcw.NewExpirableCache`
1. `Nop` - do-nothing cache

Main features:

- LoadingCache (guava style)
- Limit maximum cache size (in bytes)
- Limit maximum key size
- Limit maximum size of a value
- TTL support (`ExpirableCache` only)
- Functional style invalidation
- Nop cache provided
- Functional options
- Sane defaults

## Install and update

Expand All @@ -17,7 +27,7 @@ The library adds a thin layer on top of [lru cache](https://github.com/hashicorp
## Usage

```
cache := lcw.NewCache(lcw.MaxKeys(500), lcw.MaxCacheSize(65536), lcw.MaxValSize(200), lcw.MaxKeySize(32))
cache := lcw.NewLruCache(lcw.MaxKeys(500), lcw.MaxCacheSize(65536), lcw.MaxValSize(200), lcw.MaxKeySize(32))
val, err := cache.Get("key123", func() (lcw.Value, error) {
res, err := getDataFromSomeSource(params) // returns string
Expand All @@ -37,4 +47,4 @@ s := val.(string) // cached value
- All byte-size limits work for values implementing `lcw.Sizer` interface
- Negative limits (max options) rejected
- `lgr.Value` wraps `interface{}` and should be converted back to the concrete type.
- The implementation started as a part of [remark42](https://github.com/umputun/remark) and later on moved to [go-pkgz/rest](https://github.com/go-pkgz/rest/tree/master/cache) library.
- The implementation started as a part of [remark42](https://github.com/umputun/remark) and later on moved to [go-pkgz/rest](https://github.com/go-pkgz/rest/tree/master/cache) library and finaly generalized to become `lcw`.
5 changes: 5 additions & 0 deletions interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ type LoadingCache interface {
// Nop is do-nothing implementation of LoadingCache
type Nop struct{}

// NewNopCache makes new do-nothing cache
func NewNopCache() *Nop {
return &Nop{}
}

// Get calls fn without any caching
func (n *Nop) Get(key string, fn func() (Value, error)) (Value, error) { return fn() }

Expand Down
4 changes: 2 additions & 2 deletions interface_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

func TestNop_Get(t *testing.T) {
var coldCalls int32
c := Nop{}
c := NewNopCache()
res, err := c.Get("key1", func() (Value, error) {
atomic.AddInt32(&coldCalls, 1)
return "result", nil
Expand All @@ -29,7 +29,7 @@ func TestNop_Get(t *testing.T) {

func TestNop_Peek(t *testing.T) {
var coldCalls int32
c := Nop{}
c := NewNopCache()
res, err := c.Get("key1", func() (Value, error) {
atomic.AddInt32(&coldCalls, 1)
return "result", nil
Expand Down

0 comments on commit a4ad3fa

Please sign in to comment.