### Basic Usage Example Source: https://github.com/ilyaskriblovsky/txredisapi/blob/master/README.md A simple example demonstrating how to connect to Redis, set a key, retrieve its value, and disconnect using txredisapi. ```APIDOC ## Basic Usage Example ### Description This example shows a basic workflow of connecting to a Redis server, performing a SET and GET operation, and then disconnecting. ### Method N/A (Illustrative Python code) ### Endpoint N/A ### Parameters N/A ### Request Example ```python #!/usr/bin/env python # coding: utf-8 import txredisapi as redis from twisted.internet import defer from twisted.internet import reactor @defer.inlineCallbacks def main(): rc = yield redis.Connection() print rc yield rc.set("foo", "bar") v = yield rc.get("foo") print "foo:", repr(v) yield rc.disconnect() if __name__ == "__main__": main().addCallback(lambda ign: reactor.stop()) reactor.run() ``` ### Response #### Success Response (200) N/A (Illustrative output) #### Response Example ``` foo: 'bar' ``` ``` -------------------------------- ### Basic Redis Connection Example Source: https://github.com/ilyaskriblovsky/txredisapi/blob/master/README.md Demonstrates a simple connection, setting a key, and retrieving it using Twisted's deferred pattern. ```python #!/usr/bin/env python # coding: utf-8 import txredisapi as redis from twisted.internet import defer from twisted.internet import reactor @defer.inlineCallbacks def main(): rc = yield redis.Connection() print rc yield rc.set("foo", "bar") v = yield rc.get("foo") print "foo:", repr(v) yield rc.disconnect() if __name__ == "__main__": main().addCallback(lambda ign: reactor.stop()) reactor.run() ``` -------------------------------- ### Store and retrieve string values Source: https://context7.com/ilyaskriblovsky/txredisapi/llms.txt Demonstrates basic SET and GET operations, including expiration settings, conditional updates, and atomic get-and-set. ```python import txredisapi as redis from twisted.internet import defer, reactor @defer.inlineCallbacks def main(): rc = yield redis.Connection() # Basic set/get yield rc.set("name", "Alice") name = yield rc.get("name") print("Name:", name) # Output: Name: Alice # Set with expiration (seconds) yield rc.set("session", "data", expire=3600) # Set with expiration (milliseconds) yield rc.set("token", "abc123", pexpire=5000) # Set only if key doesn't exist (NX) result = yield rc.set("unique", "value", only_if_not_exists=True) print("Set NX result:", result) # OK or None # Set only if key exists (XX) yield rc.set("existing", "updated", only_if_exists=True) # Alternative set if not exists yield rc.setnx("lock", "holder_id") # Set with expiration (SETEX) yield rc.setex("temp", 60, "temporary_data") # Get and set atomically old_value = yield rc.getset("counter", "0") print("Old value:", old_value) yield rc.disconnect() main().addCallback(lambda _: reactor.stop()) reactor.run() ``` -------------------------------- ### Basic Key Operations with txredisapi Source: https://context7.com/ilyaskriblovsky/txredisapi/llms.txt Demonstrates basic Redis key operations like setting, checking existence, getting type, finding keys by pattern, scanning keys, and deleting keys. Use `scan` for large datasets to avoid blocking the server. ```python import txredisapi as redis from twisted.internet import defer, reactor @defer.inlineCallbacks def main(): rc = yield redis.Connection() yield rc.set("mykey", "myvalue") # Check if key exists exists = yield rc.exists("mykey") print("Exists:", exists) # Output: 1 # Get key type key_type = yield rc.type("mykey") print("Type:", key_type) # Output: string # Find keys by pattern yield rc.set("user:1", "a") yield rc.set("user:2", "b") yield rc.set("user:3", "c") keys = yield rc.keys("user:*") print("User keys:", keys) # Output: ['user:1', 'user:2', 'user:3'] # Scan keys incrementally (better for large datasets) cursor, keys = yield rc.scan(cursor=0, pattern="user:*", count=10) print("Cursor:", cursor, "Keys:", keys) # Delete keys deleted = yield rc.delete(["user:1", "user:2"]) print("Deleted:", deleted) # Output: 2 yield rc.disconnect() main().addCallback(lambda _: reactor.stop()) reactor.run() ``` -------------------------------- ### Key Management Utilities with txredisapi Source: https://context7.com/ilyaskriblovsky/txredisapi/llms.txt Provides utility functions for key management, including renaming keys (`rename`, `renamenx`), retrieving a random key (`randomkey`), and getting the database size (`dbsize`). ```python import txredisapi as redis from twisted.internet import defer, reactor @defer.inlineCallbacks def main(): rc = yield redis.Connection() yield rc.set("oldname", "value") # Rename key yield rc.rename("oldname", "newname") # Rename only if new key doesn't exist yield rc.renamenx("newname", "uniquename") # Get random key from database key = yield rc.randomkey() print("Random key:", key) # Get number of keys in database count = yield rc.dbsize() print("Key count:", count) yield rc.disconnect() main().addCallback(lambda _: reactor.stop()) reactor.run() ``` -------------------------------- ### Check txredisapi version with Cyclone Source: https://github.com/ilyaskriblovsky/txredisapi/blob/master/README.md If you have Cyclone installed, you can check the txredisapi version by importing cyclone.redis. ```python import cyclone.redis cyclone.redis.version ``` -------------------------------- ### Web Server with Lazy Redis Connections Source: https://github.com/ilyaskriblovsky/txredisapi/blob/master/README.md This Python code implements a web server using Cyclone and TxRedisAPI to expose Redis commands over HTTP. It utilizes lazy connections, ensuring the server starts even if Redis is down. All Redis operations are wrapped in try-except blocks to catch connection errors and return a 503 Service Unavailable response. ```python #!/usr/bin/env python # coding: utf-8 import sys import cyclone.web import cyclone.redis from twisted.internet import defer from twisted.internet import reactor from twisted.python import log class Application(cyclone.web.Application): def __init__(self): handlers = [ (r"/text/(.+)", TextHandler) ] RedisMixin.setup() cyclone.web.Application.__init__(self, handlers, debug=True) class RedisMixin(object): redis_conn = None @classmethod def setup(self): RedisMixin.redis_conn = cyclone.redis.lazyConnectionPool() # Provide GET, SET and DELETE redis operations via HTTP class TextHandler(cyclone.web.RequestHandler, RedisMixin): @defer.inlineCallbacks def get(self, key): try: value = yield self.redis_conn.get(key) except Exception, e: log.msg("Redis failed to get('%s'): %s" % (key, str(e))) raise cyclone.web.HTTPError(503) self.set_header("Content-Type", "text/plain") self.write("%s=%s\r\n" % (key, value)) @defer.inlineCallbacks def post(self, key): value = self.get_argument("value") try: yield self.redis_conn.set(key, value) except Exception, e: log.msg("Redis failed to set('%s', '%s'): %s" % (key, value, str(e))) raise cyclone.web.HTTPError(503) self.set_header("Content-Type", "text/plain") self.write("%s=%s\r\n" % (key, value)) @defer.inlineCallbacks def delete(self, key): try: n = yield self.redis_conn.delete(key) except Exception, e: log.msg("Redis failed to del('%s'): %s" % (key, str(e))) raise cyclone.web.HTTPError(503) self.set_header("Content-Type", "text/plain") self.write("DEL %s=%d\r\n" % (key, n)) def main(): log.startLogging(sys.stdout) reactor.listenTCP(8888, Application(), interface="127.0.0.1") reactor.run() if __name__ == "__main__": main() ``` -------------------------------- ### Create a Lazy Redis Connection Source: https://context7.com/ilyaskriblovsky/txredisapi/llms.txt Creates a lazy connection that returns the connection handler immediately without waiting for the connection to be established. Ideal for servers that should start immediately regardless of Redis availability. ```python import txredisapi as redis from twisted.internet import defer, reactor # Connection handler returned immediately - connects in background rc = redis.lazyConnection(host="localhost", port=6379, dbid=0) @defer.inlineCallbacks def handle_request(): try: value = yield rc.get("key") print("Value:", value) except redis.ConnectionError: print("Redis not available") reactor.callLater(1, handle_request) reactor.run() ``` -------------------------------- ### lazyConnection Source: https://context7.com/ilyaskriblovsky/txredisapi/llms.txt Creates a lazy connection that returns the connection handler immediately without waiting for the connection to be established. Ideal for servers that should start immediately regardless of Redis availability. ```APIDOC ## lazyConnection ### Description Creates a lazy connection that returns the connection handler immediately without waiting for the connection to be established. Ideal for servers that should start immediately regardless of Redis availability. ### Method POST (or similar, depending on underlying implementation) ### Endpoint /redis/lazyconnect ### Parameters #### Query Parameters - **host** (string) - Optional - The hostname or IP address of the Redis server. - **port** (integer) - Optional - The port number of the Redis server. - **dbid** (integer) - Optional - The database index to connect to. ### Request Example ```python import txredisapi as redis from twisted.internet import defer, reactor # Connection handler returned immediately - connects in background rc = redis.lazyConnection(host="localhost", port=6379, dbid=0) @defer.inlineCallbacks def handle_request(): try: value = yield rc.get("key") print("Value:", value) except redis.ConnectionError: print("Redis not available") reactor.callLater(1, handle_request) reactor.run() ``` ### Response #### Success Response (200) - **connectionHandler** (object) - An object representing the lazy Redis connection. ``` -------------------------------- ### Curl Command to Get a Value Source: https://github.com/ilyaskriblovsky/txredisapi/blob/master/README.md This command demonstrates how to retrieve a value associated with a key from Redis using the HTTP interface. It sends a GET request to the specified key. ```bash $ curl -D - http://localhost:8888/text/foo HTTP/1.1 200 OK Content-Length: 9 Etag: "b63729aa7fa0e438eed735880951dcc21d733676" Content-Type: text/plain foo=bar ``` -------------------------------- ### Server Management and Diagnostics with info, ping, time Source: https://context7.com/ilyaskriblovsky/txredisapi/llms.txt Demonstrates basic server commands for checking connectivity, retrieving server information, and managing databases. Use flushdb and flushall with extreme caution. ```python import txredisapi as redis from twisted.internet import defer, reactor @defer.inlineCallbacks def main(): rc = yield redis.Connection() # Ping server result = yield rc.ping() print("Ping:", result) # Output: PONG # Get server info info = yield rc.info("server") print("Redis version:", info.get("redis_version")) # Get server time timestamp, microseconds = yield rc.time() print(f"Server time: {timestamp}.{microseconds}") # Flush current database (CAREFUL!) # yield rc.flushdb() # Flush all databases (CAREFUL!) # yield rc.flushall() # Persistence commands # Synchronous save # yield rc.save() # Asynchronous save # yield rc.bgsave() # Last save timestamp lastsave = yield rc.lastsave() print("Last save:", lastsave) yield rc.disconnect() main().addCallback(lambda _: reactor.stop()) reactor.run() ``` -------------------------------- ### Establish a Redis Connection Pool Source: https://github.com/ilyaskriblovsky/txredisapi/blob/master/README.md Demonstrates initializing a connection pool, performing basic set/get operations, and handling connection errors. ```python #!/usr/bin/env python # coding: utf-8 import txredisapi as redis from twisted.internet import defer from twisted.internet import reactor def sleep(n): d = defer.Deferred() reactor.callLater(5, lambda *ign: d.callback(None)) return d @defer.inlineCallbacks def main(): rc = yield redis.ConnectionPool() print rc # set yield rc.set("foo", "bar") # sleep, so you can kill redis print "sleeping for 5s, kill redis now..." yield sleep(5) try: v = yield rc.get("foo") print "foo:", v yield rc.disconnect() except redis.ConnectionError, e: print str(e) if __name__ == "__main__": main().addCallback(lambda ign: reactor.stop()) reactor.run() ``` -------------------------------- ### Modify Sorted Sets with txredisapi Source: https://context7.com/ilyaskriblovsky/txredisapi/llms.txt Use zincrby to increment a member's score, zcard to get the number of members, and zremrangebyrank/zremrangebyscore to remove members by rank or score range. ```python import txredisapi as redis from twisted.internet import defer, reactor @defer.inlineCallbacks def main(): rc = yield redis.Connection() yield rc.delete("scores") yield rc.zadd("scores", 100, "player1", 200, "player2", 300, "player3") # Increment score new_score = yield rc.zincrby("scores", 50, "player1") print("New score:", new_score) # Output: 150.0 # Get cardinality count = yield rc.zcard("scores") print("Count:", count) # Output: 3 # Remove by rank range (lowest scores) removed = yield rc.zremrangebyrank("scores", 0, 0) print("Removed:", removed) # Output: 1 # Remove by score range removed = yield rc.zremrangebyscore("scores", 0, 200) print("Removed:", removed) yield rc.disconnect() main().addCallback(lambda _: reactor.stop()) reactor.run() ``` -------------------------------- ### Supported Connection Methods Source: https://github.com/ilyaskriblovsky/txredisapi/blob/master/README.md Lists all available connection factory methods for single, pooled, sharded, and Unix socket connections. ```python Connection(host, port, dbid, reconnect, charset) lazyConnection(host, port, dbid, reconnect, charset) ConnectionPool(host, port, dbid, poolsize, reconnect, charset) lazyConnectionPool(host, port, dbid, poolsize, reconnect, charset) ShardedConnection(hosts, dbid, reconnect, charset) lazyShardedConnection(hosts, dbid, reconnect, charset) ShardedConnectionPool(hosts, dbid, poolsize, reconnect, charset) lazyShardedConnectionPool(hosts, dbid, poolsize, reconnect, charset) UnixConnection(path, dbid, reconnect, charset) lazyUnixConnection(path, dbid, reconnect, charset) UnixConnectionPool(unix, dbid, poolsize, reconnect, charset) lazyUnixConnectionPool(unix, dbid, poolsize, reconnect, charset) ShardedUnixConnection(paths, dbid, reconnect, charset) lazyShardedUnixConnection(paths, dbid, reconnect, charset) ShardedUnixConnectionPool(paths, dbid, poolsize, reconnect, charset) lazyShardedUnixConnectionPool(paths, dbid, poolsize, reconnect, charset) ``` -------------------------------- ### Connection Methods Source: https://github.com/ilyaskriblovsky/txredisapi/blob/master/README.md Lists all available connection methods for txredisapi, including standard, pooled, sharded, and Unix socket connections, with both eager and lazy initialization options. ```APIDOC ## Connection Methods ### Description This section details the various ways to establish connections to a Redis server using txredisapi. You can choose between standard connections, connection pools, sharded connections (with automatic distribution), and Unix socket connections. Each type also offers a 'lazy' initialization option. ### Supported Connection Types - **Standard Connections:** - `Connection(host, port, dbid, reconnect, charset)` - `lazyConnection(host, port, dbid, reconnect, charset)` - **Connection Pools:** - `ConnectionPool(host, port, dbid, poolsize, reconnect, charset)` - `lazyConnectionPool(host, port, dbid, poolsize, reconnect, charset)` - **Sharded Connections:** - `ShardedConnection(hosts, dbid, reconnect, charset)` - `lazyShardedConnection(hosts, dbid, reconnect, charset)` - **Sharded Connection Pools:** - `ShardedConnectionPool(hosts, dbid, poolsize, reconnect, charset)` - `lazyShardedConnectionPool(hosts, dbid, poolsize, reconnect, charset)` - **Unix Socket Connections:** - `UnixConnection(path, dbid, reconnect, charset)` - `lazyUnixConnection(path, dbid, reconnect, charset)` - **Unix Socket Connection Pools:** - `UnixConnectionPool(unix, dbid, poolsize, reconnect, charset)` - `lazyUnixConnectionPool(unix, dbid, poolsize, reconnect, charset)` - **Sharded Unix Socket Connections:** - `ShardedUnixConnection(paths, dbid, reconnect, charset)` - `lazyShardedUnixConnection(paths, dbid, reconnect, charset)` - **Sharded Unix Socket Connection Pools:** - `ShardedUnixConnectionPool(paths, dbid, poolsize, reconnect, charset)` - `lazyShardedUnixConnectionPool(paths, dbid, poolsize, reconnect, charset)` ### Connection Arguments - **host** (string): The IP address or hostname of the Redis server. [default: localhost] - **port** (integer): Port number of the Redis server. [default: 6379] - **path** (string): Path of the Redis server's socket. [default: /tmp/redis.sock] - **dbid** (integer): Database ID of the Redis server. [default: 0] - **poolsize** (integer): Number of connections to maintain in a pool. [default: 10] - **reconnect** (boolean): Automatically reconnect if the connection is lost. [default: True] - **charset** (string): String encoding to use. Set to None to disable decoding/encoding. [default: utf-8] - **hosts** (list of strings): List of `host:port` pairs for sharded connections. [default: None] - **paths** (list of strings): List of socket pathnames for sharded Unix connections. [default: None] - **password** (string): Password for authenticating with the Redis server. [default: None] - **ssl_context_factory** (boolean or object): Use SSL/TLS. Can be a boolean or a specific `ClientContextFactory`. [default: False] ``` -------------------------------- ### Perform Basic Set Operations Source: https://context7.com/ilyaskriblovsky/txredisapi/llms.txt Demonstrates adding, removing, checking membership, and retrieving members from a Redis set. ```python import txredisapi as redis from twisted.internet import defer, reactor @defer.inlineCallbacks def main(): rc = yield redis.Connection() yield rc.delete("myset") # Add members to set yield rc.sadd("myset", "a") yield rc.sadd("myset", ["b", "c", "d"]) # Add multiple # Check membership is_member = yield rc.sismember("myset", "b") print("Is 'b' a member:", is_member) # Output: True # Get all members members = yield rc.smembers("myset") print("Members:", members) # Output: {'a', 'b', 'c', 'd'} # Get cardinality (size) size = yield rc.scard("myset") print("Size:", size) # Output: 4 # Remove members yield rc.srem("myset", "a") yield rc.srem("myset", ["b", "c"]) # Pop random member member = yield rc.spop("myset") print("Popped:", member) # Get random member without removing member = yield rc.srandmember("myset") print("Random:", member) yield rc.disconnect() main().addCallback(lambda _: reactor.stop()) reactor.run() ``` -------------------------------- ### Work with Hashes using txredisapi Source: https://context7.com/ilyaskriblovsky/txredisapi/llms.txt Use hset and hmset to set hash fields, hget and hmget to retrieve them, and hgetall to retrieve all fields and values. hsetnx sets a field only if it does not exist. ```python import txredisapi as redis from twisted.internet import defer, reactor @defer.inlineCallbacks def main(): rc = yield redis.Connection() yield rc.delete("user:1") # Set single field yield rc.hset("user:1", "name", "Alice") # Set multiple fields yield rc.hmset("user:1", { "email": "alice@example.com", "age": "30", "city": "NYC" }) # Get single field name = yield rc.hget("user:1", "name") print("Name:", name) # Output: Alice # Get multiple fields values = yield rc.hmget("user:1", ["name", "email", "age"]) print("Values:", values) # Output: ['Alice', 'alice@example.com', '30'] # Get all fields and values user = yield rc.hgetall("user:1") print("User:", user) # Output: {'name': 'Alice', 'email': 'alice@example.com', 'age': '30', 'city': 'NYC'} # Set only if field doesn't exist yield rc.hsetnx("user:1", "country", "USA") yield rc.disconnect() main().addCallback(lambda _: reactor.stop()) reactor.run() ``` -------------------------------- ### Perform multiple key operations Source: https://context7.com/ilyaskriblovsky/txredisapi/llms.txt Shows how to set or retrieve multiple keys in a single operation using MSET, MGET, and MSETNX. ```python import txredisapi as redis from twisted.internet import defer, reactor @defer.inlineCallbacks def main(): rc = yield redis.Connection() # Set multiple keys at once yield rc.mset({ "user:1:name": "Alice", "user:1:email": "alice@example.com", "user:2:name": "Bob", "user:2:email": "bob@example.com" }) # Get multiple keys at once values = yield rc.mget([ "user:1:name", "user:1:email", "user:2:name", "user:2:email" ]) print("Values:", values) # Output: Values: ['Alice', 'alice@example.com', 'Bob', 'bob@example.com'] # MSETNX - set multiple only if none exist result = yield rc.msetnx({ "new:key1": "value1", "new:key2": "value2" }) print("MSETNX result:", result) # 1 if all set, 0 if any existed yield rc.disconnect() main().addCallback(lambda _: reactor.stop()) reactor.run() ``` -------------------------------- ### Execute Blocking List Operations Source: https://context7.com/ilyaskriblovsky/txredisapi/llms.txt Demonstrates blocking list operations like blpop, brpop, and brpoplpush which wait for elements to become available. ```python import txredisapi as redis from twisted.internet import defer, reactor @defer.inlineCallbacks def main(): pool = yield redis.ConnectionPool(poolsize=2) # In a separate connection, push data after delay def push_later(): pool.rpush("queue", "task_data") reactor.callLater(2, push_later) # Blocking pop - waits up to 5 seconds for element result = yield pool.blpop("queue", timeout=5) if result: key, value = result print(f"Got from {key}: {value}") # Output: Got from queue: task_data else: print("Timeout - no data") # Blocking pop from multiple lists (returns first available) result = yield pool.blpop(["queue1", "queue2", "queue3"], timeout=5) # Blocking pop and push to another list atomically yield pool.rpush("source", "item") result = yield pool.brpoplpush("source", "destination", timeout=5) print("Moved:", result) yield pool.disconnect() main().addCallback(lambda _: reactor.stop()) reactor.run() ``` -------------------------------- ### Authenticate with a password Source: https://github.com/ilyaskriblovsky/txredisapi/blob/master/README.md Connect to Redis using the `txredisapi.Connection` class and provide the password during initialization for authentication. ```python #!/usr/bin/env python import txredisapi from twisted.internet import defer from twisted.internet import reactor @defer.inlineCallbacks def main(): redis = yield txredisapi.Connection(password="foobared") yield redis.set("foo", "bar") print (yield redis.get("foo")) reactor.stop() if __name__ == "__main__": main() reactor.run() ``` -------------------------------- ### Combine Sorted Sets with txredisapi Source: https://context7.com/ilyaskriblovsky/txredisapi/llms.txt Use zunionstore to combine sorted sets, summing scores of common members. Use zinterstore with weights and aggregation options to create intersections. ```python import txredisapi as redis from twisted.internet import defer, reactor @defer.inlineCallbacks def main(): rc = yield redis.Connection() yield rc.delete(["zset1", "zset2", "result"]) yield rc.zadd("zset1", 1, "a", 2, "b", 3, "c") yield rc.zadd("zset2", 2, "b", 3, "c", 4, "d") # Union - combine scores of common members count = yield rc.zunionstore("result", ["zset1", "zset2"]) print("Union count:", count) # Output: 4 result = yield rc.zrange("result", 0, -1, withscores=True) print("Union:", result) # Output: [('a', 1.0), ('d', 4.0), ('b', 4.0), ('c', 6.0)] # Intersection with weights count = yield rc.zinterstore( "result", {"zset1": 2, "zset2": 3}, # Multiply scores by weights aggregate="SUM" # or MIN, MAX ) result = yield rc.zrange("result", 0, -1, withscores=True) print("Weighted intersection:", result) yield rc.disconnect() main().addCallback(lambda _: reactor.stop()) reactor.run() ``` -------------------------------- ### Manage Redis Lists with Push and Pop Source: https://context7.com/ilyaskriblovsky/txredisapi/llms.txt Demonstrates adding elements to the head or tail of a list and removing them using lpush, rpush, lpop, and rpop. ```python import txredisapi as redis from twisted.internet import defer, reactor @defer.inlineCallbacks def main(): rc = yield redis.Connection() yield rc.delete("mylist") # Push to head (left) yield rc.lpush("mylist", "first") yield rc.lpush("mylist", ["second", "third"]) # Push multiple # Push to tail (right) yield rc.rpush("mylist", "last") # Pop from head value = yield rc.lpop("mylist") print("Popped from head:", value) # Output: third # Pop from tail value = yield rc.rpop("mylist") print("Popped from tail:", value) # Output: last yield rc.disconnect() main().addCallback(lambda _: reactor.stop()) reactor.run() ``` -------------------------------- ### Probabilistic Cardinality Estimation with pfadd, pfcount, pfmerge Source: https://context7.com/ilyaskriblovsky/txredisapi/llms.txt Demonstrates using HyperLogLog commands to estimate unique elements in large datasets. Requires Redis 3.2+. ```python import txredisapi as redis from twisted.internet import defer, reactor @defer.inlineCallbacks def main(): rc = yield redis.Connection() yield rc.delete(["visitors:day1", "visitors:day2", "visitors:total"]) # Add elements to HyperLogLog yield rc.pfadd("visitors:day1", ["user1", "user2", "user3", "user4"]) yield rc.pfadd("visitors:day1", "user5") # Add single element yield rc.pfadd("visitors:day2", ["user3", "user4", "user5", "user6", "user7"]) # Count unique elements (approximate) count1 = yield rc.pfcount("visitors:day1") print("Day 1 unique visitors:", count1) # Output: ~5 count2 = yield rc.pfcount("visitors:day2") print("Day 2 unique visitors:", count2) # Output: ~5 # Count across multiple HyperLogLogs total = yield rc.pfcount(["visitors:day1", "visitors:day2"]) print("Total unique visitors:", total) # Output: ~7 # Merge HyperLogLogs yield rc.pfmerge("visitors:total", ["visitors:day1", "visitors:day2"]) merged_count = yield rc.pfcount("visitors:total") print("Merged count:", merged_count) yield rc.disconnect() main().addCallback(lambda _: reactor.stop()) reactor.run() ``` -------------------------------- ### Execute Redis Transactions in Python Source: https://github.com/ilyaskriblovsky/txredisapi/blob/master/README.md Shows how to use MULTI and commit to perform transactions. Transactions are not supported on sharded connections. ```python #!/usr/bin/env python # coding: utf-8 import txredisapi as redis from twisted.internet import defer from twisted.internet import reactor @defer.inlineCallbacks def main(): rc = yield redis.ConnectionPool() # Remove the keys yield rc.delete(["a1", "a2", "a3"]) # Start transaction t = yield rc.multi() # These will return "QUEUED" - even t.get(key) yield t.set("a1", "1") yield t.set("a2", "2") yield t.set("a3", "3") yield t.get("a1") # Try to call get() while in a transaction. # It will fail if it's not a connection pool, or if all connections # in the pool are in a transaction. # Note that it's rc.get(), not the transaction object t.get(). try: v = yield rc.get("foo") print "foo=", v except Exception, e: print "can't get foo:", e # Commit, and get all responses from transaction. r = yield t.commit() print "commit=", repr(r) yield rc.disconnect() if __name__ == "__main__": main().addCallback(lambda ign: reactor.stop()) reactor.run() ``` -------------------------------- ### Pipeline commands for batch execution Source: https://github.com/ilyaskriblovsky/txredisapi/blob/master/README.md Use the `pipeline` method to accumulate commands and send them to the server in a single batch. This is effective for loading large amounts of data. Group commands in batches of around 10k to balance performance and memory usage. ```python #!/usr/bin/env python # coding: utf-8 import txredisapi as redis from twisted.internet import defer from twisted.internet import reactor @defer.inlineCallbacks def main(): rc = yield redis.ConnectionPool() # Start grouping commands pipeline = yield rc.pipeline() pipeline.set("foo", 123) pipeline.set("bar", 987) pipeline.get("foo") pipeline.get("bar") # Write those 2 sets and 2 gets to redis all at once, and wait # for all replies before continuing. results = yield pipeline.execute_pipeline() print "foo:", results[2] # should be 123 print "bar:", results[3] # should be 987 yield rc.disconnect() if __name__ == "__main__": main().addCallback(lambda ign: reactor.stop()) reactor.run() ``` -------------------------------- ### Query Sorted Sets by Score Source: https://context7.com/ilyaskriblovsky/txredisapi/llms.txt Demonstrates querying sorted sets using score ranges, pagination, counting, and rank retrieval. ```python import txredisapi as redis from twisted.internet import defer, reactor @defer.inlineCallbacks def main(): rc = yield redis.Connection() yield rc.delete("scores") yield rc.zadd("scores", 10, "a", 20, "b", 30, "c", 40, "d", 50, "e") # Get members within score range members = yield rc.zrangebyscore("scores", 20, 40) print("Score 20-40:", members) # Output: ['b', 'c', 'd'] # With scores and pagination members = yield rc.zrangebyscore( "scores", min="-inf", max="+inf", withscores=True, offset=1, count=2 ) print("Paginated:", members) # Output: [('b', 20.0), ('c', 30.0)] # Reverse order by score members = yield rc.zrevrangebyscore("scores", 40, 20, withscores=True) print("Reverse:", members) # Output: [('d', 40.0), ('c', 30.0), ('b', 20.0)] # Count members in score range count = yield rc.zcount("scores", 20, 40) print("Count 20-40:", count) # Output: 3 # Get score of member score = yield rc.zscore("scores", "c") print("Score of 'c':", score) # Output: 30 # Get rank (0-based position) rank = yield rc.zrank("scores", "c") print("Rank of 'c':", rank) # Output: 2 # Get reverse rank rev_rank = yield rc.zrevrank("scores", "c") print("Reverse rank:", rev_rank) # Output: 2 yield rc.disconnect() main().addCallback(lambda _: reactor.stop()) reactor.run() ``` -------------------------------- ### Batch commands with pipeline Source: https://context7.com/ilyaskriblovsky/txredisapi/llms.txt Use pipelines to buffer commands and execute them in a single batch for improved performance. This is ideal for bulk operations. ```python import txredisapi as redis from twisted.internet import defer, reactor @defer.inlineCallbacks def main(): rc = yield redis.Connection() # Start pipeline - commands are buffered, not sent immediately pipeline = yield rc.pipeline() # Queue commands (no network round-trips yet) pipeline.set("key1", "value1") pipeline.set("key2", "value2") pipeline.set("key3", "value3") pipeline.get("key1") pipeline.get("key2") pipeline.get("key3") pipeline.incr("counter") # Execute all commands in single batch results = yield pipeline.execute_pipeline() print("Results:", results) # Output: ['OK', 'OK', 'OK', 'value1', 'value2', 'value3', 1] # Pipelines are great for bulk operations pipeline = yield rc.pipeline() for i in range(1000): pipeline.set(f"bulk:{i}", f"value_{i}") yield pipeline.execute_pipeline() print("Bulk insert complete") yield rc.disconnect() main().addCallback(lambda _: reactor.stop()) reactor.run() ``` -------------------------------- ### Connect to Redis Sentinel for master discovery and failover Source: https://github.com/ilyaskriblovsky/txredisapi/blob/master/README.md Use `txredisapi.Sentinel` to discover Redis master and slave addresses and automatically handle failover. Specify Sentinel addresses in a list of tuples. The `master_for` method returns a connection to the master, and `slave_for` connects to a slave. ```python #!/usr/bin/env python from twisted.internet.task import react import txredisapi @defer.inlineCallbacks def main(reactor): sentinel = txredisapi.Sentinel([("sentinel-a", 26379), ("sentinel-b", 26379), ("sentinel-c", 26379)]) redis = sentinel.master_for("service_name") yield redis.set("foo", "bar") print (yield redis.get("foo")) yield redis.disconnect() yield sentinel.disconnect() react(main) ``` -------------------------------- ### Incremental Scanning with scan, sscan, hscan, zscan Source: https://context7.com/ilyaskriblovsky/txredisapi/llms.txt Shows how to iterate over keys, set members, hash fields, and sorted set elements incrementally using cursor-based scanning. Useful for large collections. ```python import txredisapi as redis from twisted.internet import defer, reactor @defer.inlineCallbacks def main(): rc = yield redis.Connection() # Create test data for i in range(100): yield rc.set(f"user:{i}", f"data_{i}") yield rc.sadd("myset", [f"member{i}" for i in range(50)]) yield rc.hmset("myhash", {f"field{i}": f"value{i}" for i in range(50)}) yield rc.zadd("myzset", *[x for i in range(50) for x in (i, f"member{i}")]) # Scan keys cursor = 0 all_keys = [] while True: cursor, keys = yield rc.scan(cursor=cursor, pattern="user:*", count=20) all_keys.extend(keys) if cursor == 0: break print(f"Found {len(all_keys)} user keys") # Scan set members cursor = 0 all_members = [] while True: cursor, members = yield rc.sscan("myset", cursor=cursor, count=10) all_members.extend(members) if cursor == 0: break print(f"Found {len(all_members)} set members") # Scan hash fields cursor = 0 all_fields = {} while True: cursor, data = yield rc.hscan("myhash", cursor=cursor, count=10) # data is [field1, value1, field2, value2, ...] for i in range(0, len(data), 2): all_fields[data[i]] = data[i+1] if cursor == 0: break print(f"Found {len(all_fields)} hash fields") # Scan sorted set cursor = 0 all_items = [] while True: cursor, items = yield rc.zscan("myzset", cursor=cursor, count=10) all_items.extend(items) if cursor == 0: break print(f"Found {len(all_items)} sorted set items") yield rc.disconnect() main().addCallback(lambda _: reactor.stop()) reactor.run() ``` -------------------------------- ### Pipeline / execute_pipeline Source: https://context7.com/ilyaskriblovsky/txredisapi/llms.txt Batch commands for improved performance using pipelines. ```APIDOC ## POST /pipeline/execute_pipeline ### Description Executes a batch of Redis commands in a single network round-trip for improved performance. Commands are buffered and sent together. ### Method POST ### Endpoint /pipeline/execute_pipeline ### Parameters #### Request Body - **commands** (array) - Required - A list of Redis commands to be executed in the pipeline. ### Request Example ```json { "commands": [ {"command": "set", "args": ["key1", "value1"]}, {"command": "get", "args": ["key1"]} ] } ``` ### Response #### Success Response (200) - **results** (array) - A list containing the results of each command executed in the pipeline. #### Response Example ```json { "results": ["OK", "value1"] } ``` ``` -------------------------------- ### Integrate with Cyclone web framework Source: https://context7.com/ilyaskriblovsky/txredisapi/llms.txt Builds a REST API using Cyclone and txredisapi, utilizing a lazy connection pool for Redis operations. ```python import cyclone.web import txredisapi as redis from twisted.internet import defer, reactor from twisted.python import log import sys class RedisMixin: redis_conn = None @classmethod def setup(cls): cls.redis_conn = redis.lazyConnectionPool(poolsize=10) class KeyValueHandler(cyclone.web.RequestHandler, RedisMixin): @defer.inlineCallbacks def get(self, key): try: value = yield self.redis_conn.get(key) if value is None: raise cyclone.web.HTTPError(404, "Key not found") self.write({"key": key, "value": value}) except redis.ConnectionError: raise cyclone.web.HTTPError(503, "Redis unavailable") @defer.inlineCallbacks def put(self, key): value = self.get_argument("value") expire = self.get_argument("expire", None) try: if expire: yield self.redis_conn.setex(key, int(expire), value) else: yield self.redis_conn.set(key, value) self.write({"status": "ok", "key": key}) except redis.ConnectionError: raise cyclone.web.HTTPError(503, "Redis unavailable") @defer.inlineCallbacks def delete(self, key): try: deleted = yield self.redis_conn.delete(key) self.write({"deleted": deleted}) except redis.ConnectionError: raise cyclone.web.HTTPError(503, "Redis unavailable") class Application(cyclone.web.Application): def __init__(self): RedisMixin.setup() handlers = [ (r"/api/kv/(.+)", KeyValueHandler), ] cyclone.web.Application.__init__(self, handlers) if __name__ == "__main__": log.startLogging(sys.stdout) reactor.listenTCP(8888, Application()) reactor.run() ``` -------------------------------- ### Connect to Redis with txredisapi Source: https://context7.com/ilyaskriblovsky/txredisapi/llms.txt Establishes a single TCP connection to a Redis server. Supports custom options like host, port, database ID, auto-reconnect, character set, password, and timeouts. ```python import txredisapi as redis from twisted.internet import defer, reactor @defer.inlineCallbacks def main(): # Basic connection to localhost:6379 rc = yield redis.Connection() # Connection with custom options rc = yield redis.Connection( host="localhost", port=6379, dbid=0, # Database index reconnect=True, # Auto-reconnect on failure charset="utf-8", # String encoding password="secret", # Redis AUTH password connectTimeout=10, # Connection timeout in seconds replyTimeout=5 # Command timeout in seconds ) yield rc.set("foo", "bar") value = yield rc.get("foo") print("foo:", value) # Output: foo: bar yield rc.disconnect() main().addCallback(lambda _: reactor.stop()) reactor.run() ```