Skip to content

Commit 4e39bce

Browse files
committed
first commit
1 parent 96a7da0 commit 4e39bce

1 file changed

Lines changed: 120 additions & 47 deletions

File tree

README.md

Lines changed: 120 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,43 @@ A lightweight, in-memory Database implemented in C# that provides a subset of Re
66

77
RedisClone is a .NET implementation of a Redis-like key-value store that supports various data types and operations. It's designed to be used as a local cache or for testing purposes where a full Redis instance isn't required.
88

9+
## Project Structure
10+
11+
```
12+
RedisClone/
13+
├── src/
14+
│ ├── RedisClone.Core/ # Core interfaces and models
15+
│ ├── RedisClone.Infrastructure/ # Implementation of Redis operations
16+
│ └── RedisClone.API/ # REST API layer
17+
└── tests/
18+
├── RedisClone.UnitTests/ # Unit tests for individual components
19+
└── RedisClone.IntegrationTests/ # Integration tests for the entire system
20+
```
21+
922
## Features
1023

1124
### Data Types
12-
- **Strings**: Basic key-value storage
13-
- **Lists**: Ordered collections of strings
14-
- **Sets**: Unordered collections of unique strings
15-
- **Hashes**: Field-value pairs
25+
- **Strings**: Basic key-value storage with support for bit operations
26+
- **Lists**: Ordered collections of strings with operations on both ends
27+
- **Sets**: Unordered collections of unique strings with set operations
28+
- **Hashes**: Field-value pairs with atomic operations
1629
- **Sorted Sets**: Ordered collections of unique strings with associated scores
30+
- **Pub/Sub**: Publish/Subscribe messaging system
31+
- **Transactions**: Support for atomic operations
1732

1833
### Key Operations
19-
- `SetAsync`: Store a string value
34+
- `SetAsync`: Store a string value with optional expiration
2035
- `GetAsync`: Retrieve a string value
2136
- `DeleteAsync`: Remove a key
2237
- `ExistsAsync`: Check if a key exists
2338
- `TTLAsync`: Get time to live for a key
2439
- `SetExpirationAsync`: Set expiration time for a key
2540
- `RemoveExpirationAsync`: Remove expiration time from a key
41+
- `IncrementAsync`/`DecrementAsync`: Atomic counter operations
42+
- `AppendAsync`: Append to string value
43+
- `GetRangeAsync`/`SetRangeAsync`: String manipulation
44+
- `StrLenAsync`: Get string length
45+
- Bit operations: `SetBitAsync`, `GetBitAsync`, `BitCountAsync`, `BitOpAsync`, `BitPosAsync`
2646

2747
### List Operations
2848
- `LPushAsync`: Add elements to the head of a list
@@ -32,38 +52,63 @@ RedisClone is a .NET implementation of a Redis-like key-value store that support
3252
- `LRangeAsync`: Get a range of elements from a list
3353
- `LIndexAsync`: Get an element from a list by index
3454
- `LLenAsync`: Get the length of a list
55+
- `LSetAsync`: Set element at index
56+
- `LRemAsync`: Remove elements from list
57+
- `LTrimAsync`: Trim list to specified range
3558

3659
### Set Operations
3760
- `SAddAsync`: Add members to a set
3861
- `SRemAsync`: Remove members from a set
3962
- `SMembersAsync`: Get all members of a set
4063
- `SIsMemberAsync`: Check if a value is a member of a set
64+
- `SCardAsync`: Get set cardinality
65+
- `SPopAsync`: Remove and return random member
66+
- `SRandMemberAsync`: Get random member(s)
67+
- Set operations: `SInterStoreAsync`, `SUnionStoreAsync`, `SDiffStoreAsync`
4168

4269
### Hash Operations
4370
- `HSetAsync`: Set a hash field
4471
- `HGetAsync`: Get a hash field
4572
- `HGetAllAsync`: Get all fields and values in a hash
46-
- `HDelAsync`: Delete a hash field
73+
- `HDelAsync`: Delete hash field(s)
74+
- `HExistsAsync`: Check if field exists
75+
- `HIncrementByAsync`: Increment field value
76+
- `HKeysAsync`: Get all hash fields
77+
- `HLenAsync`: Get number of fields
78+
- `HValsAsync`: Get all hash values
4779

4880
### Sorted Set Operations
4981
- `ZAddAsync`: Add members to a sorted set
5082
- `ZRemAsync`: Remove members from a sorted set
51-
- `ZRangeAsync`: Get a range of members from a sorted set
52-
- `ZRevRangeAsync`: Get a range of members from a sorted set in reverse order
83+
- `ZScoreAsync`: Get member score
84+
- `ZIncrementByAsync`: Increment member score
85+
- `ZCardAsync`: Get set cardinality
86+
- `ZCountAsync`: Count members in score range
87+
- `ZRangeAsync`/`ZRevRangeAsync`: Get range of members
88+
- `ZRangeByScoreAsync`/`ZRevRangeByScoreAsync`: Get members by score range
89+
- `ZRankAsync`/`ZRevRankAsync`: Get member rank
90+
- `ZRemRangeByRankAsync`/`ZRemRangeByScoreAsync`: Remove members by rank/score
91+
- Set operations: `ZUnionStoreAsync`, `ZInterStoreAsync`
5392

5493
### Pub/Sub Operations
5594
- `PublishAsync`: Publish a message to a channel
5695
- `SubscribeAsync`: Subscribe to a channel
5796
- `UnsubscribeAsync`: Unsubscribe from a channel
5897

98+
### Transaction Support
99+
- `BeginTransactionAsync`: Start a transaction
100+
- `CommitTransactionAsync`: Commit the transaction
101+
- `RollbackTransactionAsync`: Rollback the transaction
102+
59103
## Architecture
60104

61-
The project is structured into several layers:
105+
The project follows a clean architecture pattern with clear separation of concerns:
62106

63107
### Core Layer (`RedisClone.Core`)
64108
- Contains interfaces and models
65109
- Defines the contract for Redis operations
66110
- Includes data models like `RedisValue` for storing different types of data
111+
- Provides the public API that users interact with
67112

68113
### Infrastructure Layer (`RedisClone.Infrastructure`)
69114
- Implements the core functionality
@@ -74,13 +119,39 @@ The project is structured into several layers:
74119
- `HashOperations`: Hash-specific operations
75120
- `SortedSetOperations`: Sorted set-specific operations
76121
- `RedisStore`: Main class that coordinates all operations
122+
- Uses `ConcurrentDictionary` for thread-safe storage
123+
- Implements proper locking mechanisms for atomic operations
124+
125+
### API Layer (`RedisClone.API`)
126+
- Provides a REST API interface to the Redis functionality
127+
- Handles HTTP requests and responses
128+
- Maps API endpoints to Redis operations
77129

78130
## Thread Safety
79131

80132
RedisClone is designed to be thread-safe:
81133
- Uses `ConcurrentDictionary` for the main store
82134
- Implements proper locking mechanisms for operations
83135
- Handles concurrent access to shared resources
136+
- All operations are atomic where required
137+
- Uses async/await for non-blocking operations
138+
139+
## Testing
140+
141+
The project includes comprehensive testing:
142+
143+
### Unit Tests
144+
- Tests individual components in isolation
145+
- Covers all data types and operations
146+
- Tests edge cases and error conditions
147+
- Verifies thread safety
148+
- Tests transaction behavior
149+
150+
### Integration Tests
151+
- Tests the entire system working together
152+
- Verifies API endpoints
153+
- Tests real-world scenarios
154+
- Ensures proper interaction between components
84155

85156
## Usage
86157

@@ -98,64 +169,66 @@ await redis.SetAsync("mykey", "myvalue");
98169

99170
// Retrieve the value
100171
string? value = await redis.GetAsync("mykey");
101-
102-
// Set with expiration
103-
await redis.SetAsync("tempkey", "tempvalue", TimeSpan.FromMinutes(5));
104172
```
105173

106-
### List Operations
174+
### List Operations Example
107175

108176
```csharp
109177
// Add items to a list
110178
await redis.RPushAsync("mylist", "item1", "item2", "item3");
111179

112-
// Get list length
113-
var length = await redis.LLenAsync("mylist");
114-
115-
// Get item at index
116-
var item = await redis.LIndexAsync("mylist", 1);
180+
// Get items from the list
181+
var items = await redis.LRangeAsync("mylist", 0, -1);
117182
```
118183

119-
### Set Operations
184+
### Hash Operations Example
120185

121186
```csharp
122-
// Add members to a set
123-
await redis.SAddAsync("myset", "member1", "member2");
187+
// Set hash fields
188+
await redis.HSetAsync("myhash", "field1", "value1");
189+
await redis.HSetAsync("myhash", "field2", "value2");
190+
191+
// Get all hash fields and values
192+
var hash = await redis.HGetAllAsync("myhash");
193+
```
194+
195+
### Sorted Set Example
124196

125-
// Check membership
126-
bool isMember = await redis.SIsMemberAsync("myset", "member1");
197+
```csharp
198+
// Add items to a sorted set
199+
await redis.ZAddAsync("myset", ("member1", 1.0), ("member2", 2.0));
127200

128-
// Get all members
129-
var members = await redis.SMembersAsync("myset");
201+
// Get items in score range
202+
var items = await redis.ZRangeByScoreAsync("myset", 0, 2);
130203
```
131204

132-
## Testing
205+
### Pub/Sub Example
206+
207+
```csharp
208+
// Subscribe to a channel
209+
await redis.SubscribeAsync("mychannel", async (message) => {
210+
Console.WriteLine($"Received: {message}");
211+
});
133212

134-
The project includes comprehensive unit tests covering:
135-
- Basic key-value operations
136-
- List operations
137-
- Set operations
138-
- Hash operations
139-
- Sorted set operations
140-
- Pub/Sub functionality
141-
- Expiration handling
142-
143-
Run tests using:
144-
```bash
145-
dotnet test
213+
// Publish a message
214+
await redis.PublishAsync("mychannel", "Hello, Redis!");
146215
```
147216

217+
## Performance Considerations
218+
219+
- All operations are designed to be efficient
220+
- Uses in-memory storage for fast access
221+
- Implements proper locking to minimize contention
222+
- Supports atomic operations for consistency
223+
- Uses async/await for non-blocking operations
224+
148225
## Limitations
149226

150-
- In-memory storage only (no persistence)
151-
- Single-instance only (no clustering)
152-
- Subset of Redis commands implemented
153-
- No support for Redis-specific features like:
154-
- Lua scripting
155-
- Streams
156-
- Geo commands
157-
- HyperLogLog
158-
- Bit operations
227+
- Data is not persisted (in-memory only)
228+
- No replication support
229+
- Limited subset of Redis commands
230+
- No authentication/authorization
231+
- No clustering support
159232

160233
## Contributing
161234

0 commit comments

Comments
 (0)