Introduction to Redis: Data Caching, Installation, and Python API Usage
This article introduces Redis as a key‑value caching system, compares it with MongoDB and Memcache, explains installation, shows how to use the redis‑py Python client—including direct connections, connection pools, and basic data‑type operations—and provides code examples for setting and retrieving values.
Data caching systems: MongoDB stores data directly on disk (persistent), Redis provides semi‑persistent storage in memory and disk, and Memcache keeps data only in memory.
Redis is a key‑value storage system that supports value types such as string, list, set, sorted set (zset), and hash; all these types support atomic operations like push/pop, add/remove, and set intersections, unions, and differences.
Installation and startup instructions can be found in the referenced article “Mac environment installing Redis”.
For PyCharm users, install the Redis module to enable development.
Redis‑py API usage can be divided into connection methods (including connection pools) and data operations (string, hash, list, set, sorted set), as well as pipeline usage and publish/subscribe patterns.
Redis provides two main client classes: StrictRedis, which implements most official commands, and Redis, a subclass kept for backward compatibility.
Example of a simple connection:
import redis
r = redis.Redis(host='10.10.2.14', port=6379)
r.set('name', 'jack')
print(r.get('name').decode())The connection pool manages all connections to a Redis server, reducing the overhead of repeatedly creating and releasing connections; multiple Redis instances can share a single pool.
Example using a connection pool:
pool = redis.ConnectionPool(host='10.10.2.14', port=6379)
r = redis.Redis(connection_pool=pool)
r.set('name', 'jack')
print(r.get('name'))Follow the public account for further research on Python interface automation.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
