In memory temporary cache written in C with ZeroMQ providing the network interface, for GNU/Linux.
This is by no means a finished production ready solution for caching data, there is much still to do.
You will need ZeroMQ 3.x installed. Optionally you may want Tiny CDB library installed for snapshotting.
> ./configure
> make
Currently no install target, as there is no production ready code to use.
> tmpcache_host dir waddress raddress
Where waddress (for writing) and raddress (for reading) is an zmq adddress in one of the following forms.
- ipc:///tmp/write.tmpcache
- tcp://127.0.0.1:8888
dir is the directory (absolute path) where the cache will reside. To improve the performance, use a tmpfs for the cache directory.
You can run numerous copies of tmpcache provided you use different network addresses. For example, you can run many read-only instances, each with a different network address and a single write-only instance.
Create a tmpfs
> sudo mount -t tmpfs -o size=64m tmpfs /srv/tmpcache
> sudo chown tmpcache:tmpcache /srv/tmpcache
Run tmpcache instance for writing on an inter-process communication address.
> tmpcache_write /srv/tmpcache ipc:///tmp/run/tmpcache.write &
Now run a read instace on a TCP network address
> tmpcache_read /srv/tmpcache tcp://localhost:9898
tmpcache can be stopped by sending a TERM signal. If you wish to run both services within a single process, you can use host instead:
> tmpcache_host /srv/tmpcache ipc:///tmp/run/tmpcache.write
tcp://localhost:9898 &
The write address is listed first. Benefits of a single process, include easier management, but also that the journal (still in development) will use a single publish address for both read and write services.
> tmpcache_dump /srv/tmpcache ipc:///tmp/run/tmpcache2.write
Using ezcdb to create a cdb file
> tmpcache_dump /srv/tmpcache | ezcdb make cache.cdb
We can then use the new cdb file as a read-only cache
> tmpcache_read cache.cdb tcp://localhost:9898
The default read will connect to the read service, ask for the data of the key supplied (foo in the example) and wait for a reply. The default timeout for the wait is 1 second.
> tmpcache_readfrom foo tcp://localhost:9898
We can change the timeout and the number of tries
> tmpcache_readfrom --tries=3 --timeout=3 foo tcp://localhost:9898
Now the maximum amount of time for an answer from the read service will be 9 seconds. We can redirect the output as standard.
> tmpcache_readfrom --tries=3 --timeout=1 foo tcp://localhost:9898 > foo
There are currently 2 modes of operation, the default timeout mode and the blocking mode. The timeout version is useful if you are unsure that the service is available or stable. Whilst the blocking version will wait until there is a response. Blocking is, in theory, faster, to enable blocking mode:
> tmpcache_readfrom --blocking foo tcp://localhost:9898
In this case, tries and timeout options do not have any affect, so are not required.
We can run a throughput test with the test option, this will generate a unique key (in the form, keyN, where N is a number, less than supplied).
> time tmpcache_readfrom --test=1000 foo tcp://localhost:9898
Will test with keys in the range, foo0 to foo999.
> tmpcache_writeto foo ipc:///tmp/run/tmpcache.write
> [contents] <enter>
Or via a file
> cat somefile | tmpcache_writeto foo ipc:///tmp/run/tmpcache.write
Delete is a special form of write, for tmpcache it simply a zero length data part.
> tmpcache_writeto foo ipc:///tmp/run/tmpcache.write
> <enter>
The shorthand is to use the delete flag
> tmpcache_writeto --delete foo ipc:///tmp/run/tmpcache.write
The following (crude) bash script collects all the seperate tmpcache_ commands togeather, and adds a little sugar.
#! /bin/bash
## tmpcache : simple prog runner
args=("$@")
shift 1
case ${args[0]} in
'hostread')
tmpcache_read --syslog "$@"
;;
'hostwrite')
tmpcache_write --syslog "$@"
;;
'host')
tmpcache_host --syslog "$@"
;;
'dump')
tmpcache_dump --cdb "$@"
;;
'read')
tmpcache_readfrom --blocking "$@"
;;
'write')
tmpcache_writeto "$@"
;;
'delete')
tmpcache_writeto --delete "$@"
;;
esac