Redis Cache Implementation with Python
This challenge focuses on integrating Redis, a popular in-memory data store, into a Python application to implement a caching layer. Caching frequently accessed data can significantly improve application performance by reducing the load on databases or other slower data sources. You will build a simple caching system using the redis-py library.
Problem Description
You are tasked with creating a Python module named redis_cache that provides a basic caching functionality using Redis. The module should include the following:
- Initialization: A class
RedisCachethat takes a Redis connection URL (e.g., 'redis://localhost:6379') as an argument during initialization. This URL will be used to connect to the Redis server. get(key)method: This method should retrieve a value from the cache using the providedkey. If the key exists in the cache, it should return the corresponding value. If the key does not exist, it should returnNone.set(key, value, expiry=None)method: This method should store avaluein the cache under the givenkey. Ifexpiry(in seconds) is provided, the key-value pair should be set to expire after the specified time. Ifexpiryis not provided, the key-value pair should persist indefinitely.delete(key)method: This method should remove a key-value pair from the cache given thekey.- Error Handling: The code should gracefully handle potential connection errors to the Redis server. If a connection error occurs during any operation, the method should return
Noneand log an error message (usingprintfor simplicity in this challenge).
Examples
Example 1:
Input:
redis_url = 'redis://localhost:6379'
cache = RedisCache(redis_url)
cache.set('mykey', 'myvalue', expiry=60)
result = cache.get('mykey')
print(result)
Output:
myvalue
Explanation: The key 'mykey' is set with the value 'myvalue' and an expiry of 60 seconds. The get method retrieves the value 'myvalue' from the cache.
Example 2:
Input:
redis_url = 'redis://localhost:6379'
cache = RedisCache(redis_url)
result = cache.get('nonexistent_key')
print(result)
Output:
None
Explanation: The key 'nonexistent_key' does not exist in the cache, so the get method returns None.
Example 3: (Edge Case - Connection Error)
Input:
redis_url = 'redis://invalid_host:6379'
cache = RedisCache(redis_url)
result = cache.get('somekey')
print(result)
Output:
None
Explanation: The Redis URL is invalid, resulting in a connection error. The get method returns None, and an error message is printed to the console.
Constraints
- The Redis server must be running on
localhost:6379by default. The URL can be overridden. - The
expiryparameter insetmust be a non-negative integer. If a negative value is provided, it should be treated as 0 (no expiry). - The code should be reasonably efficient. Avoid unnecessary operations.
- The
redis-pylibrary must be used. - Assume the Redis server is accessible and functional unless a connection error occurs.
Notes
- You'll need to install the
redis-pylibrary:pip install redis - Consider using a
try...exceptblock to handle potential connection errors when initializing the Redis connection. - The
setexcommand in Redis can be used to set a key with an expiry time. - Focus on the core caching functionality. Error logging can be simple (using
print). - The challenge is to demonstrate understanding of Redis integration, not to build a production-ready caching system. Simplicity and clarity are prioritized.