lhotakj / Kasi

Python based lightweight and fast key-value socket cache server supporting any object with optional expiration. Supports multiple store domains. Methods .get() .set() .delete ().reset() and .shutdown()

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Kasi

Kasi means is Shona language "speed" and it's a cache server implementation similar to redis. I wrote this code to serve a simple caching server to help me to store persisent data such as sessions for a python web server. The intention is not to compete redis or memcached, but to provide a lightweight solution for cases where you can't install software. When running complex tests, Kasi is about 2-3x slower than redis, the difference is noticeable only when executing hundreds of thousands requests; the performance should be sufficient for basic use cases.

The cache server supports storing of any type of object, it's not limited to str like redis. There's build-in algortithm to perform operation with strings much faster.

The server can host multiple so called domains (like db in redis). If not specificed, the default domain default is be used. Deamon mode is not available yet, coming soon :)

Basic methods of the client are as follows:

  • .set(name, value, timedelta=None, domain="default") - stores a key with name and value with expiration timedelta in domain domain. Value can be any Python object.
  • .get(name, domain="default") - gets the value of key name. If the key doesn't exists or has expired, returns None
  • .delete(name, domain="default") - removes the key name.
  • .reset() - resets server. Note all domains got purged. TODO optional parameter domain
  • .shutdown() - shutdowns server. In case you want take control over the process.

Requirements

  • Python 3.6+ (Python 2 support is currently ongoing)
  • 3pp dependencies

Demo

Server

Start Kasi server bound to 0.0.0.0 in one console

from kasi import Server
Server.start_server(host='0.0.0.0', port=5000)

Client

and run this code in another console.

from kasi import Client
from datetime import datetime, timedelta

# create an instance of client
client = Client.Client(host='localhost', port=5000)
client.reset()
    
# save string into domain X
client.set("D1", "test1", domain="X")
print(str(client.get("D1", domain="X")))

# save non-trivial object
client.set("list", ['a', 'd'], timedelta(seconds=1))               # set expiration 1 second
client.set("text", 'hello', timedelta(hours=1), domain="default")  # set expiration 1 hour and save into domain 'default'
client.set("unicode1", u'2H₂ + O₂ ⇌ 2H₂O, R = 4.7 kΩ, ⌀ 200 mm')   # store unicode
client.set("unicode2", u'ก ข ฃ ค ฅ ฆ ง จ ฉ ช ซ ฌ ญ ฎ ฏ ')        # store unicode

# get them
print(str(client.get("list")))
print(str(client.get("text")))
print(str(client.get("unicode1")))
print(str(client.get("unicode2")))
print(str(client.get("not-existing")))    # demonstrate reading non-existing key

client.delete("D1", domain="X")
print(str(client.stats()))

You may shutdown the server by calling. Some more security checks may be implemented in the future

client.shutdown() 

See folder demo to run this code. You can also try performance.py to see the difference between Kasi and redis.

CircleCI Known Vulnerabilities

Running server in docker container

In project directory run the following command:

docker build -t example/kasi:1.0 .

Then run the server ina an ephemeral container:

docker run -it --rm -p 5000:5000 example/kasi:1.0

About

Python based lightweight and fast key-value socket cache server supporting any object with optional expiration. Supports multiple store domains. Methods .get() .set() .delete ().reset() and .shutdown()

License:Apache License 2.0


Languages

Language:Python 95.4%Language:Shell 2.8%Language:Dockerfile 1.8%