Skip to content

Commit

Permalink
Merge pull request #33 from Attumm/v2.3.1
Browse files Browse the repository at this point in the history
Readme changes

Added example of redis encryption
Added example of using preserve timeout for keys, that was developed with @cterrazas2
  • Loading branch information
Attumm authored Jan 13, 2024
2 parents 68d025c + 04be854 commit f947b69
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 8 deletions.
26 changes: 19 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ By leveraging Redis for efficient key-value storage, RedisDict allows for high-p
* Dictionary-like interface: Use familiar Python dictionary syntax to interact with Redis.
* Data Type Support: Comprehensive support for various data types, including strings, integers, floats, booleans, lists, dictionaries, sets, and tuples.
* Pipelining support: Use pipelines for batch operations to improve performance.
* Expiration support: Set expiration times for keys using context managers.
* Expiration Support: Enables the setting of expiration times either globally or individually per key, through the use of context managers.
* Efficiency and Scalability: RedisDict is designed for use with large datasets and is optimized for efficiency. It retrieves only the data needed for a particular operation, ensuring efficient memory usage and fast performance.
* Namespace Management: Provides simple and efficient namespace handling to help organize and manage data in Redis, streamlining data access and manipulation.
* Distributed Computing: With its ability to seamlessly connect to other instances or servers with access to the same Redis instance, RedisDict enables easy distributed computing.
Expand Down Expand Up @@ -56,21 +56,30 @@ Redis provides a valuable feature that enables keys to expire. RedisDict support
1. Set a default expiration time when creating a RedisDict instance. In this example, the keys will have a default expiration time of 10 seconds.

```python
dic = RedisDict(namespace='app_name', expire=10)
dic = RedisDict(expire=10)
dic['gone'] = 'in ten seconds'
```
2. Temporarily set the default expiration time within the scope using a context manager. In this example, the key 'gone' will expire after 60 seconds. The default expiration time for other keys outside the context manager remains unchanged.

```python
from redis_dict import RedisDict

dic = RedisDict(namespace='bar')
dic = RedisDict()

seconds = 60
with dic.expire_at(seconds):
dic['gone'] = 'in sixty seconds'
```

3. Updating keys while preserving the initial timeout In certain situations, there is a need to update the value while keeping the expiration intact. This is achievable by setting the 'preserve_expiration' to true.

```python
dic = RedisDict(expire=10, preserve_expiration=True)
dic['gone'] = 'in ten seconds'

time.sleep(5)
dic['gone'] = 'gone in 5 seconds'

```

### Batching
Efficiently batch your requests using the Pipeline feature, which can be easily utilized with a context manager.

Expand Down Expand Up @@ -185,13 +194,16 @@ dic.setdefault("d", 4)
print(dic["d"]) # Output: 4
```


### Additional Examples
For more advanced examples of RedisDict, please refer to the unit-test files in the repository. All features and functionalities are thoroughly tested in [unit tests (here)](https://github.com/Attumm/redis-dict/blob/main/tests.py#L1) Or take a look at load test for batching [load test](https://github.com/Attumm/redis-dict/blob/main/load_test.py.py#L1).
The unit-tests can be as used as a starting point.

### Tests
### Redis Encryption
Setup guide for configuring and utilizing encrypted Redis for redis-dict.
[Setup guide](https://github.com/Attumm/redis-dict/blob/main/encrypted_redis.MD)


### Tests
The RedisDict library includes a comprehensive suite of tests that ensure its correctness and resilience. The test suite covers various data types, edge cases, and error handling scenarios. It also employs the Hypothesis library for property-based testing, which provides fuzz testing to evaluate the implementation

## Installation
Expand Down
80 changes: 80 additions & 0 deletions encrypted_redis.MD
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# Setup Guide encrypted Redis for Redis-dict

### Introduction

In this document, we will do the following:
1. Creating a Self-Signed Certificate: This is the initial step of our guide.
2. Setting Up Redis with Docker and TLS: Next, we will set up an encrypted Docker Redis instance that will use the created certificate.
3. Python Code: After setting up, we will install Redis-dict.
4. Running the Test: Finally, we will configure Redis-dict to use the encrypted Redis instance.
For production use, one might already have a Redis instance and/or certificates."

### Steps

1. **Creating Self-Signed Certificate**:

Use OpenSSL to generate a self-signed certificate:

```bash
openssl req -x509 -newkey rsa:4096 -keyout test_redis_key.pem -out test_redis_cert.pem -days 365 -nodes
```

This command will create a private key `test_redis_key.pem` and a self-signed certificate `test_redis_cert.pem`.

2. **Setting Up Redis with Docker and TLS**:

Create a `redis.conf` file:

```bash
echo "port 6379
tls-port 6380
tls-cert-file /tls/test_redis_cert.pem
tls-key-file /tls/test_redis_key.pem
tls-ca-cert-file /tls/test_redis_cert.pem
" > redis.conf
```

Run a Redis Docker container with the self-signed certificate and key:

```bash
docker run -v `pwd`:/tls -v `pwd`/redis.conf:/usr/local/etc/redis/redis.conf -p 6379:6379 -p 6380:6380 redis redis-server /usr/local/etc/redis/redis.conf
```

3. **Python Code**:

Install Python Redis package, if not already installed:

```bash
pip install redis-dict
```

Create a `main.py` Python file with the following code:

```python
from redis_dict import RedisDict

redis_config = {
host: 'localhost',
port: 6380,
ssl: True,
ssl_keyfile=: 'test_redis_key.pem',
ssl_certfile=: 'test_redis_cert.pem',
ssl_cert_reqs: 'required',
ssl_ca_certs: 'test_redis_cert.pem'
}
dic = RedisDict(redis_kwargs=redis_config)


dic['foo'] = 'bar'
print(dic['foo']) # should return 'bar'
```

This will connect to the Redis instance via TLS and set a key-value pair, then retrieve and print the value.

4. **Running the Test**:

Run the Python script:

```bash
python main.py
```
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
long_description=long_description,
long_description_content_type='text/markdown',

version='2.3.0',
version='2.3.1',
py_modules=['redis_dict'],
install_requires=['redis',],
license='MIT',
Expand Down

0 comments on commit f947b69

Please sign in to comment.