
The idea of writing
a cache server came to me a long time ago, but there was no suitable occasion and tool to start working on it. I wanted to make my cache server for two reasons:
experience , the ability to easily add the functions I need (those who have seen the memcached code will understand me). The main problem for me was C ++. Despite the fact that I often come across him, I don’t like him at all. I will not start holivar and write about its shortcomings. I don’t like him like kefir since childhood: we are incompatible with it. So for me, the big news was the existence of
FreePascal . Once upon a time I had experience in Delphi, so with Pascal I was “on you”. I was particularly surprised that FreePascal was cross-platform (which Delphi didn’t shine with).
What is needed for the cache server? Basically, a
hash table ,
network access to it, and tools for
deleting obsolete entries .
To implement the
hash table, I chose a binary tree (perhaps not the best way) with resolving collisions based on
chains . The network interface was implemented using non-blocking sockets.
To work with the server, I threw a simple text protocol of three commands: GET, PUT, REMOVE.
')
Request data by key:
GET <key-len> <key> \ r \ nAnswer options:
- NODATA \ r \ n
- DATA <data-len> <data> \ r \ n
Post data to server:
PUT <key-len> <key> <data-len> \ r \ n
<data> \ r \ nAnswer options:
- SUCCESS \ r \ n
- HIT \ r \ n (if the transferred data matches the data in the cache)
- FAILURE \ r \ n
Delete data:
REMOVE <key-len> <key> \ r \ nAnswer options:
- SUCCESS \ r \ n
- FAILURE \ r \ n
Problems began with deletion: multi-threaded access to a binary tree can lead to data corruption. Here we must think and come up with interesting solutions.
In the hope of an exciting collaborative development, I posted my development on github. I really hope that I will find talented programmers among users of Habr. Project address on github:
github.com/mdevils/encachedI would be grateful if you tell me a good way to organize a stream-safe hash table.
Server development plans
New commands: APPEND, PREPEND, INC, DEC, CAS (Compare and Swap), STAT, TAG (tag entry).
Full support for multi-threaded write / read without locks.
Control of occupied memory.
Work under Windows.
Now the daemon works on port 2332 under Linux (x86, x86_64), Mac OS X (x86, x86_64). It will probably work under BSD, I have no opportunity to test.
The server is going as usual with the make command. Runs with the argument "-r". FreePascal version 2.4.0 must be installed on the machine.
PS Happy New Year!