Wednesday, October 9, 2024

Cache server in 30 lines


This is a cache server that can add, delete and query key/value pairs, with their number limited only by available memory.

We'll use "index" type, which is a high-performance data structure. For example, with 1,000,000 keys it will take only about 20 comparisons to find any key; and the range search is just one hop. Index is based on a modified AVL/B tree.

Create new "index" application first, in a new directory (you can name it anything you like):
mkdir -p index
cd index

The mgrg command is a Gliimly service manager and here it will create a new application named "index" (it can be different from the directory it's in):
sudo mgrg -i -u $(whoami) index

Create a source code file "srv.gliim":
vi srv.gliim

and copy and paste this:
 begin-handler /srv public
     do-once
         new-index ind process-scope
     end-do-once
     get-param op
     get-param key
     get-param data
     if-true op equal "add"
         write-index ind key (key) value data status st
         if-true st equal GG_ERR_EXIST
             @Key exists [<<p-out key>>]
         else-if
             @Added [<<p-out key>>]
         end-if
     else-if op equal "delete"
         delete-index ind key (key) value val status st
         if-true st equal GG_ERR_EXIST
             @Not found [<<p-out key>>]
         else-if
             @Deleted, old value was [<<p-out val>>]
         end-if
     else-if op equal "query"
         read-index ind equal (key) value val status st
         if-true st equal GG_ERR_EXIST
             @Not found, queried [<<p-out key>>]
         else-if
             @Value [<<p-out val>>]
         end-if
     end-if
 end-handler

A service will run as a single process because each operation is handled very fast, even with large number of concurrent requests.
Build a service
gg -q

Run as service
mgrg -w 1 index

The above will start a single server process (-w 1) to serve incoming requests.
Test it
This is a bash test script to insert 3 keys into your cache server, query them, then delete them. Create "test_tree" file:
vi test_tree

And copy/paste the following:
#Add 3 key/data pairs. Key value is 0,1,2... and data values are "data_0", "data_1", "data_2" etc.
for i in {1..3}; do
   gg -r --req="/srv/op=add/key=$i/data=data_$i" --exec --service --app="/index" --silent-header
done
echo "Keys added"

#Query all 3 keys and check that values retrieved are the correct ones.
for i in {1..3}; do
   gg -r --req="/srv/op=query/key=$i" --exec --service --app="/index" --silent-header
done
echo "Keys queried"

#Delete all 3 keys
ERR="0"
for i in {1..3}; do
   gg -r --req="/srv/op=delete/key=$i" --exec --service --app="/index" --silent-header
done
echo "Keys deleted"

Make sure it's executable and run it:
chmod +x test_tree
./test_tree

The result is this:
Added [1]
Added [2]
Added [3]
Keys added
Value [data_1]
Value [data_2]
Value [data_3]
Keys queried
Deleted, old value was [data_1]
Deleted, old value was [data_2]
Deleted, old value was [data_3]
Keys deleted