raphanus

package module
v0.14.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 23, 2024 License: MIT Imports: 10 Imported by: 2

README

Raphanus - simple in-memory cache

Go Reference Go Coverage Status Docker Pulls Sourcegraph Report Card

Install

From source:

go get -u github.com/msoap/raphanus
# build server & cli
cd $GOPATH/src/github.com/msoap/raphanus/server && go build -o $GOPATH/bin/raphanus-server
# cli client not implement now
cd $GOPATH/src/github.com/msoap/raphanus/cli && go build -o $GOPATH/bin/raphanus-cli

From Docker hub:

docker pull msoap/raphanus

Run server

raphanus-server [options]
options:
  -address string
       	address for bind server (default "localhost:8771")
  -auth string
       	user:password for enable HTTP basic authentication
  -filename string
       	file name for storage on disk, '' - for work in-memory only
  -sync-time int
       	time in seconds between sync on disk
  -version
       	get version

in Docker container:

docker run --name raphanus --publish 8771:8771 --detach msoap/raphanus

Examples: get calls to server by curl

  • get count of keys: curl -s 'http://localhost:8771/v1/length'
  • get keys: curl -s 'http://localhost:8771/v1/keys'
  • get stat with authentication: curl -s -u user:pass 'http://localhost:8771/v1/stat'
  • set integer key k1 with ttl (100 sec): curl -s -X POST -d 123 'http://localhost:8771/v1/int/k1?ttl=100'
  • get integer key k1: curl -s 'http://localhost:8771/v1/int/k1'
  • set string key k2 without ttl: curl -s -X POST -d 'str value' 'http://localhost:8771/v1/str/k2'
  • get string key k2: curl -s 'http://localhost:8771/v1/str/k2'
  • set list value: curl -s -X POST -H 'Content-Type: application/json' -d '["v1", "v2"]' http://localhost:8771/v1/list/k3
  • get list value: curl -s 'http://localhost:8771/v1/list/k3'
  • set dict value: curl -s -X POST -H 'Content-Type: application/json' -d '{"dk1": "v1", "dk2": "v2"}' http://localhost:8771/v1/dict/k4
  • get dict value: curl -s 'http://localhost:8771/v1/dict/k4'
  • delete key k1: curl -s -X DELETE http://localhost:8771/v1/remove/k1
  • see other in handlers.go

Use as embed DB

import (
    "github.com/msoap/raphanus"
    "github.com/msoap/raphanus/common"
)

func main() {
    raph := raphanus.New()
    // or with storage, with sync every 300 seconds
    // raph := raphanus.New().SetStorage("filename.db", 300)
    raph.SetStr("key", "value")
    v, err := raph.GetStr("key")
    if err == raphanuscommon.ErrKeyNotExists {
        ...
    }

    raph.UnderLock(func () {
        v, err := raph.GetStr("k1")
        if err != nil {
            return
        }
        raph.SetStr("k1", v + " updated", 0)
    })
}

Use client library for connect with server

GoDoc

import (
    "github.com/msoap/raphanus/client"
    "github.com/msoap/raphanus/common"
)

func main() {
	// with default address:
	raph := raphanusclient.New()
	// or with another address:
	// raph := raphanusclient.New(raphanusclient.Cfg{Address: "http://localhost:8771"})
	// or with authentication:
	// raph := raphanusclient.New(raphanusclient.Cfg{User: "uname", Password: "pass"})

    raph.SetStr("key", "value", 3600)
    v, err := raph.GetStr("key")
    if err == raphanuscommon.ErrKeyNotExists {
        ...
    }
}

An example of using the library: simple.go

Bencmarks:

with servers in Docker

$ docker run --name raphanus --publish 8771:8771 --rm msoap/raphanus
$ docker run --name redis --rm --publish 6379:6379 redis
$ docker run --name memcache --rm --publish 11211:11211 memcached
$ make run-benchmark

Benchmark_raphanusServer-4      	    5000	   1196515 ns/op	    9432 B/op	     128 allocs/op
Benchmark_raphanusEmbed-4       	 5000000	      1633 ns/op	     184 B/op	       4 allocs/op
Benchmark_redis-4               	   10000	    636079 ns/op	     343 B/op	      19 allocs/op
Benchmark_memcache-4            	   10000	    689439 ns/op	    2744 B/op	      63 allocs/op
Benchmark_raphanusServerTTL-4   	    5000	   1178499 ns/op	    9459 B/op	     130 allocs/op
Benchmark_raphanusEmbedTTL-4    	 5000000	      2554 ns/op	     194 B/op	       6 allocs/op
Benchmark_redisTTL-4            	   10000	    791056 ns/op	     417 B/op	      21 allocs/op
Benchmark_memcacheTTL-4         	   10000	    733611 ns/op	    2744 B/op	      63 allocs/op

local raphanus and redis servers (on MacOS)

Redis 3.2.7

memcached 1.4.34

$ make run-benchmark
Benchmark_raphanusServer-4      	   20000	    349394 ns/op	    9465 B/op	     131 allocs/op
Benchmark_raphanusEmbed-4       	 3000000	      1919 ns/op	     213 B/op	       7 allocs/op
Benchmark_boltdb-4              	   30000	    258030 ns/op	   34320 B/op	      69 allocs/op
Benchmark_redis-4               	   50000	    128278 ns/op	     411 B/op	      23 allocs/op
Benchmark_memcache-4            	   50000	    135533 ns/op	    2752 B/op	      63 allocs/op
Benchmark_raphanusServerTTL-4   	   20000	    356989 ns/op	    9499 B/op	     133 allocs/op
Benchmark_raphanusEmbedTTL-4    	 2000000	      3347 ns/op	     244 B/op	       9 allocs/op
Benchmark_redisTTL-4            	   50000	    127851 ns/op	     473 B/op	      25 allocs/op
Benchmark_memcacheTTL-4         	   50000	    135326 ns/op	    2811 B/op	      67 allocs/op

Documentation

Overview

Package raphanus - simple implementation of in-memory cache

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DB

type DB struct {
	*sync.RWMutex
	// contains filtered or unexported fields
}

DB - in-memory cache object

func New

func New() DB

New - get new cache object

func (*DB) DecrInt

func (db *DB) DecrInt(key string) (err error)

DecrInt - decrement integer value on 1

func (*DB) GetBytes

func (db *DB) GetBytes(key string) ([]byte, error)

GetBytes - get []byte value by key

func (*DB) GetDict

func (db *DB) GetDict(key string) (value raphanuscommon.DictValue, err error)

GetDict - get dict value by key

func (*DB) GetDictItem

func (db *DB) GetDictItem(key string, dictKey string) (string, error)

GetDictItem - get item from dict value by exists key

func (*DB) GetInt

func (db *DB) GetInt(key string) (int64, error)

GetInt - get integer value by key

func (*DB) GetList

func (db *DB) GetList(key string) (value raphanuscommon.ListValue, err error)

GetList - get list value by key

func (*DB) GetListItem

func (db *DB) GetListItem(key string, index int) (string, error)

GetListItem - get one item from list value by key

func (*DB) GetStr

func (db *DB) GetStr(key string) (string, error)

GetStr - get string value by key

func (*DB) IncrInt

func (db *DB) IncrInt(key string) (err error)

IncrInt - increment integer value on 1

func (*DB) Keys

func (db *DB) Keys() []string

Keys - get all keys from cache (as []string)

func (*DB) Len

func (db *DB) Len() int

Len - return length of cache

func (*DB) Remove

func (db *DB) Remove(key string) (err error)

Remove - remove key from cache

func (*DB) RemoveDictItem

func (db *DB) RemoveDictItem(key, dictKey string) error

RemoveDictItem - remove item on dict value by exists key

func (*DB) SetBytes

func (db *DB) SetBytes(key string, value []byte, ttl int) error

SetBytes - create/update []byte value by key

func (*DB) SetDict

func (db *DB) SetDict(key string, value raphanuscommon.DictValue, ttl int) error

SetDict - create/update dict value by key

func (*DB) SetDictItem

func (db *DB) SetDictItem(key, dictKey, dictValue string) error

SetDictItem - set item on dict value by exists key

func (*DB) SetInt

func (db *DB) SetInt(key string, value int64, ttl int) error

SetInt - create/update integer value by key

func (*DB) SetList

func (db *DB) SetList(key string, value raphanuscommon.ListValue, ttl int) error

SetList - create/update list value by key

func (*DB) SetListItem

func (db *DB) SetListItem(key string, index int, value string) error

SetListItem - set one item of list value by key

func (*DB) SetStorage

func (db *DB) SetStorage(fsStorageName string, fsStorageSyncTime int) DB

SetStorage - setup persistent storage

func (*DB) SetStr

func (db *DB) SetStr(key, value string, ttl int) error

SetStr - create/update string value by key

func (*DB) UnderLock

func (db *DB) UnderLock(fn func())

UnderLock - execute few RW-methods under one Lock

func (*DB) UpdateBytes

func (db *DB) UpdateBytes(key string, value []byte) (err error)

UpdateBytes - update []byte value by exists key

func (*DB) UpdateDict

func (db *DB) UpdateDict(key string, value raphanuscommon.DictValue) (err error)

UpdateDict - update dict value by exists key

func (*DB) UpdateInt

func (db *DB) UpdateInt(key string, value int64) (err error)

UpdateInt - update integer value by exists key

func (*DB) UpdateList

func (db *DB) UpdateList(key string, value raphanuscommon.ListValue) (err error)

UpdateList - update list value by exists key

func (*DB) UpdateStr

func (db *DB) UpdateStr(key, value string) (err error)

UpdateStr - update string value by exists key

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL