Skip to content

sinomoe/gocache

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

🦜 Gocache

MIT PkgGoDev

gocache is a distributed caching library, adapted from groupcache, intended as a replacement for memcached in many cases.

Installation

Install and update this package with go get -u github.com/sinomoe/gocache.

Usage

  1. define getter function(used when cache missed) implement Getter interface

    getFromDB := gocache.GetterFunc(func(key string) ([]byte, error) {
        data, err :=  db.Get(key) // get data from database by the key
        if err == nil {
            return data, nil
        }
    	return nil, fmt.Errorf("%s not exist", key)
    })
  2. initialize a group named as "scores" with max 2048 cached items

    goc := gocache.NewGroup("scores", 2<<10, getFromDB)
  3. create httpPool instance using local addr

    peers := gocache.NewHTTPPool("http://localhost:8001")
  4. set peers on httpPool instance, there are three peers, every addr is meant to a gocache peer

    peers.SetPeers("http://localhost:8002", "http://localhost:8002", "http://localhost:8003")
  5. register httpPool(peers) instance on group instance

    goc.RegisterPeers(peers)

License

MIT © sino

About

🦜 A distributed caching library, adapted from groupcache.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages