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 withname
andvalue
with expirationtimedelta
in domaindomain
. Value can be any Python object..get(name, domain="default")
- gets the value of keyname
. If thekey
doesn't exists or has expired, returnsNone
.delete(name, domain="default")
- removes the keyname
..reset()
- resets server. Note all domains got purged. TODO optional parameterdomain
.shutdown()
- shutdowns server. In case you want take control over the process.
- Python 3.6+ (Python 2 support is currently ongoing)
- 3pp dependencies
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)
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
.
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