A in-memory cache server implementation inspired by Redis, written in Go and compatible with Redis clients, clis and etc, this project built just for fun 😊
- TCP server implementation
- In-memory key-value storage
- Support for TTL (Time To Live) on keys
- Concurrent connections handling
- Thread-safe operations
- Supports RESP (Redis Serialization Protocol)
git clone https://github.com/vesal-j/gocache.git
cd gocache
go build
./gocache
The server will start listening on port 6380 by default.
Since GoCache uses RESP (Redis Serialization Protocol), you should use redis-cli
to connect to the server:
redis-cli -p 6380
Or connect to a specific host:
redis-cli -h localhost -p 6380
Once connected with redis-cli, you can use standard Redis commands:
# Set a key-value pair
SET mykey myvalue
OK
# Set a key with TTL (expires in 10 seconds)
SET mykey myvalue EX 10
OK
# Get a value
GET mykey
myvalue
# Check if key exists
EXISTS mykey
(integer) 1
# Get TTL for a key
TTL mykey
(integer) 8
# After 10 seconds, the key expires
GET mykey
(nil)
# Test connection
PING
PONG
# Get server info
INFO
# ... server information ...
# Get database size
DBSIZE
(integer) 0
Since GoCache implements the RESP protocol, it's compatible with most Redis clients:
- Python:
redis-py
- Node.js:
redis
orioredis
- Java:
Jedis
- C#:
StackExchange.Redis
Example with Python:
import redis
r = redis.Redis(host='localhost', port=6380)
r.set('mykey', 'myvalue', ex=10)
value = r.get('mykey')
print(value) # b'myvalue'
internal/core/app.go
- Main application logic and TCP server implementationinternal/store
- In-memory storage implementation with TTL supportinternal/command
- Command handlers (GET, SET, etc.)internal/core/router.go
- Command routing and processinginternal/utils
- RESP protocol utilities
- Concurrent Connections: Uses goroutines to handle multiple client connections
- TTL Support: Automatic key expiration with background cleanup
- Thread-safe Operations: Uses mutex for safe concurrent access
- Command Router: Extensible command routing system
- Clean Architecture: Modular design for easy extension
- RESP Protocol: Full Redis Serialization Protocol support for client compatibility
- Go 1.23.1 or higher
- redis-cli (for testing) or any Redis client library
This project is open source and available under the MIT License.
Contributions are welcome! Please feel free to submit a Pull Request.