Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Caching With C

A minimal Redis-like cache server written in C.

This project is for people who want to understand how a networked cache works under the hood: sockets, select, RESP parsing, command dispatch, an in-memory key-value store, and TTL handling without hiding the interesting parts behind a framework.

Fork it if you want a compact C systems project that is still small enough to read in one sitting and practical enough to extend.

What It Does

  • Accepts TCP clients with a single-threaded select loop
  • Speaks the Redis Serialization Protocol subset used by normal Redis clients
  • Supports PING, SET, GET, DEL, EXPIRE, and TTL
  • Stores string keys and values in memory
  • Deletes expired keys lazily when they are touched
  • Includes focused C tests for the store, RESP parser, and command layer

Quick Start

Run these commands from the repository root:

cd server
make clean
make all
make test
./build/server 6379

In another terminal, talk to it with redis-cli:

redis-cli -p 6379 PING
redis-cli -p 6379 SET hello world
redis-cli -p 6379 GET hello
redis-cli -p 6379 EXPIRE hello 10
redis-cli -p 6379 TTL hello
redis-cli -p 6379 DEL hello

Expected replies:

PONG
OK
"world"
(integer) 1
(integer) 10
(integer) 1

Build And Test Rules

Use the Makefile in server/. The project intentionally keeps the build simple and explicit.

cd server
make all      # builds ./build/server
make test     # runs all test binaries
make clean    # removes generated build files

Before opening a pull request or sharing a fork, run:

cd server
make clean && make all && make test

The generated server/build/ directory is ignored by git. Do not commit compiled binaries.

Project Layout

server/
  Makefile
  src/
    main.c              # CLI entrypoint: ./server <port>
    headers/            # Public module interfaces
    core/
      server.c          # Socket lifecycle and select loop
      client.c          # Per-client input buffers
      resp.c            # RESP parser and response formatting
      commands.c        # Command dispatch and command behavior
      store.c           # In-memory key-value store with TTL
  tests/
    test_store.c
    test_resp.c
    test_commands.c

Protocol Scope

The server currently supports RESP arrays of bulk strings, which is what redis-cli sends for normal commands.

Supported commands:

  • PING [message]
  • SET key value
  • GET key
  • DEL key [key ...]
  • EXPIRE key seconds
  • TTL key

The following iterations will cover:

  • Persistence
  • Replication
  • Authentication
  • Transactions
  • Inline Redis commands
  • Advanced Redis data structures

Contributing

Keep changes small and testable.

For behavior changes, add or update a test in server/tests/ first, then run:

cd server
make clean && make all && make test

If that command does not pass, the change is not ready to share.

About

A minimalist implementation of the RESP protocol used to facilitate a minimalist redis server developed in C.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages