Skip to content

pygaur/pyredis

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

pyredis

A Python API to interact with Redis. It implements common Redis data structures and methods. Useful to separate Redis connection handling from business logic in your projects.

How to Use

Import the SmartCache object into your file:

>>> from smartcache.redis_communicator import SmartCache

Hash Operations

  1. hset (set a field in a hash):
>>> cache = SmartCache()
>>> cache[('test', 'key')] = "value"
>>> True
  1. hget (get a field from a hash):
>>> cache = SmartCache()
>>> out = cache[('test', 'key')]
>>> b'value'

Key Existence Checks

  1. exists (check if a key exists):
>>> cache = SmartCache()
>>> 'test' in cache
>>> True
  1. hexists (check if a field exists in a hash):
>>> cache = SmartCache()
>>> ('test', 'key') in cache
>>> True

Set Operations

  1. sadd and smembers (adding to a set and retrieving all members):
>>> cache = SmartCache()
>>> cache['one'] = 'data'   # sadd
>>> cache['one']            # smembers
>>> {b'data'}
>>> cache['one'] = 'data1'  # sadd
>>> cache['one']            # smembers
>>> {b'data', b'data1'}

List Operations

  1. Adding elements to a list:
>>> cache = SmartCache()
>>> cache.rpush('mylist', 'first')  # Add to the end
>>> cache.rpush('mylist', 'second')
>>> cache.lpush('mylist', 'start')  # Add to the beginning
>>> cache['mylist']                 # Get all list elements
>>> [b'start', b'first', b'second']
  1. Removing elements from a list:
>>> cache = SmartCache()
>>> cache.lpop('mylist')    # Remove from the beginning
>>> b'start'
>>> cache.rpop('mylist')    # Remove from the end
>>> b'second'
>>> cache['mylist']
>>> [b'first']

Configuration

By default, SmartCache connects to Redis at:

  • Host: 127.0.0.1
  • Port: 6379
  • DB: 0

You can configure these values:

>>> cache = SmartCache(host='redis.example.com', port=6380, db=1)

Context Manager Support

SmartCache can be used as a context manager to ensure proper cleanup:

>>> with SmartCache() as cache:
...     cache['key'] = 'value'
...     print(cache['key'])
b'value'

About

A python api to interact with redis .It implements all redis data structures and method.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages