A simple, in-memory TCP cache server written in TypeScript for Deno. It provides basic Redis-like SET and GET commands with a time-to-live (TTL) for keys. The project is designed to be a learning tool for building a network service in Deno.
- In-Memory Cache: Stores key-value pairs in a
Mapdata structure for fast access. - Time-to-Live (TTL): Keys can be set with an expiration time in seconds.
- Background Cleanup: A simple, non-blocking cleanup loop automatically removes expired keys.
- Simple TCP Protocol: Communicates using a basic, RESP-like protocol for easy client interaction.
- File Dumping: Supports a
DUMPcommand to write the current cache to a JSON file.
- Deno v1.34.0 or higher.
- Clone this repository or create the files locally.
- Open your terminal and navigate to the project directory.
- Run the server with the following command. The
--allow-netand--allow-writeflags are necessary to allow the server to listen on a port and write the dump file.
deno run --allow-net --allow-write cheap_cache_server.ts
The server will start and listen on port 6379.
You can interact with the server using any TCP client, such as netcat or telnet.
Using netcat (nc)
- Open a new terminal window.
nc localhost 6379
- Type a command followed by
Enter. The server's response will be displayed on the next line.
| Command | Description | Example |
|---|---|---|
SET <key> <value> <ttl> |
Sets a key-value pair with a TTL in seconds. | SET user:123 "Alice" 3600 |
GET <key> |
Retrieves the value for a key. Returns $-1\r\n if not found or expired. |
GET user:123 |
DUMP [filename] |
Dumps all non-expired keys to a JSON file. Defaults to cheap_cache_dump.json. |
DUMP or DUMP my_data.json |
# Set a key 'mykey' with value 'hello' that expires in 10 seconds
> SET mykey hello 10
+OK
# Get the value
> GET mykey
$5
hello
# Wait 10 seconds, then try to get it again
> GET mykey
$-1
# Dump the cache to a file
> DUMP my_backup.json
+OK
You can use the PHP script I included to see how it would be used in a PHP client. PHP is not dead.
This project is a learning playground built in Deno. Its purpose is not to be a full Redis replacement, but rather to explore and experiment with:
- TCP networking in Deno using
Deno.Connand async loops. - Simple caching with key/value pairs and TTLs.
- LRU eviction for both cache entries and client connections.
- Persistent connections for clients (tested via PHP scripts).
The PHP client and other utilities are meant to demonstrate usage and test behaviors, not for production use.
This project is primarily educational — it shows how connections, caching, and evictions can work together in a small Redis-like system.
This cheapcache server is a lightweight, in-memory key-value store with TTL and LRU eviction. It is best suited for ephemeral, non-sensitive data:
-
✅ Content / UI caching: Cache HTML fragments, JSON responses, or rendered templates to speed up page loads.
-
✅ Session metadata**: Store user preferences, temporary selections, or other non-sensitive session info.
-
✅ Computed values / aggregate: Cache precomputed totals, counts, or analytics to avoid repeated expensive calculations.
⚠️ Avoid storing secrets, passwords, JWT tokens, or large binary data. This cache is volatile and primarily meant for temporary, fast-access data.