A Redis inspired key value caching server built using Node.js and Socket.IO
- Node.js
- Socket.io
- PostgreSQL
- bcrypt
- dotenv
A map is used to store cache with an upper limit of 10000 items (tweakable). The signup for the cache is done using curl REST requests but the normal get,set and delete operations use sockets.
Authentication is done once using middleware during to socket handshake by querying a database to check a hashed API key (10 salt rounds) with auth data sent during connection. The user data, including allowed rooms is attached to the socket (resembles a session) and is used for authorization when a socket tries get or set or delete to check if a user can access the room it wishes to.
The cache has both LRU eviction implemented using a doubly linked list as well as a TTL based eviction which scans the cache map every 10 minutes and evicts expired data. The map stores the corresponding node in the lru list for access without traversal. The project first used only REST requests but to prevent repeated handshakes it was naturally shifted to use sockets
Data is stored in the map as roomName:key eg: chatApplication:message:1
Data is stored as a string but one can simply send non string data and upon retrieval process it back to an object.
npm install node server.js
this runs the server on localhost:3000, edit the code for a different host or port
for the CLI to test the server, node client.js
this needs the server to be running and will prompt for the command.
- get roomName key
- set roomName key value ttl
- delete roomName key
socket.emit("get",{key:key}); socket.emit("set",{key:key,value:value,ttl:ttl}) //Can be json in value but may need client side parsing for str to json socket.emut("delete",{key:key})