Skip to main content

RedisEntities

How to install it?

You can install RedisEntites from this Github repository with python3 setup.py install, or just install it directly from pypi with pip3 install redis-entities.

What is it?

Redis Entities is a small library that allows you to map represent certain entities in Redis. An Entity could be for example a Hashmap type and stores information about one User that currently requested a password reset. Each Entity has a predefined Prefix used to differentiate the different types of Entities you create. There are 3 different RedisEntities you can use:

  • RedisListEntity
  • RedisSetEntity
  • RedisHashmapEntity

In addition there are a couple of mixins, which can be used to provide additional functionality to your entities. There are 3 different Mixins you can use:

  • HashedIdentifierMixin: All identifiers are hashed before stored in Redis
  • AuthenticatedEncryptionMixin: All values are encrypted and authenticated before stored in Redis
  • DeterministicAuthenticatedEncryptionMixin: All values are encrypted and authenticated deterministically (the same value is encrypted to the same ciphertext) before stored in Redis

You can also combine multiple Mixins, except AuthenticatedEncryptionMixin and DeterministicAuthenticatedEncryptionMixin.

How does it work?

To create an own Entity, just subclass from one of the Provided RedisEntity Base classes.
Note, that every Entity has to set the RedisClient Class Attribute to an instances of Redis from redis-py, or a class that supports all methods that Redis from redis-py does.

Under the hood all keys are stored as strings, and all values are stored as bytes in Redis.

RedisListEntity:

import redis
from redis_entities import RedisListEntity
from redis_entities.mixins import AuthenticatedEncryptionMixin
class JobQueueEntity(RedisListEntity, AuthenticatedEncryptionMixin):
    RedisClient = redis.Redis(...)
    Prefix = "JobQueue"
    AesKey = b"deaddeaddeaddeaddeaddeaddeaddead" # AES-128 in this case

JobQueueEntity.lpush("Worker1", "command1")
assert JobQueueEntity.length("Worker1") == 1

Supported Methods are:

  • lpush
  • brpop
  • lindex
  • length
  • clear

RedisSetEntity:

import redis
from redis_entities import RedisSetEntity
from redis_entities.mixins import DeterministicAuthenticatedEncryptionMixin
class AccessTokensEntity(RedisSetEntity, DeterministicAuthenticatedEncryptionMixin):
    RedisClient = redis.Redis(...)
    Prefix = "AccessTokens"
    Expire = 180
    AesKey = b"deaddeaddeaddeaddeaddeaddeaddeaddeaddeaddeaddeaddeaddeaddeaddead" # need to be twice the size of the key required by the underlying cipher (e.g. 64 bytes for AES-256)

AccessTokensEntity.add("User1", "Token1")
AccessTokensEntity.add("User2", "Token1")
assert AccessTokensEntity.exists("User1", "Token1") is True

Supported Methods are:

  • add
  • delete
  • clear
  • exists
  • list_all
  • length

NOTE: It is strongly recommended to use the DeterministicAuthenticatedEncryptionMixin instead of the AuthenticatedEncryptionMixin when you are working with RedisSetEntity , as most methods need to know the exact value that is stored in Redis. With the DeterministicAuthenticatedEncryptionMixin , the same plaintext results in the same ciphertext and thus makes this possible.

RedisHashmapEntity

import redis
from redis_entities import RedisHashmapEntity, 
from redis_entities.mixins import HashedIdentifierMixin, AuthenticatedEncryptionMixin
class VerifyEmailTokens(RedisHashmapEntity, HashedIdentifierMixin, AuthenticatedEncryptionMixin):
    RedisClient = redis.Redis(...)
    Prefix = "VerifyEmailTokens"
    Contents = (
        "MandatoryKey1",
        "MandatoryKey2"
    )
    Expire = 180
    HashName = "sha512_256"
    Salt = b"VerifyEmailTokens"
    AesKey = b"deaddeaddeaddeaddeaddeaddeaddead"

VerifyEmailTokens.store("test@example.com", MandatoryKey1="Value1", MandatoryKey2="Value2")
loaded_entity = VerifyEmailTokens.load("test@example.com")
assert loaded_entity.MandatoryKey1 == b"Value1"
assert loaded_entity.MandatoryKey1 == b"Value2"
assert VerifyEmailTokens.exists("test@example.com") is True

Supported Methods are:

  • store

  • load

  • exists

  • delete

  • length

Metadata

Release files for redis-entities 1.0.8

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for redis-entities 1.0.8
File Size Uploaded
redis_entities-1.0.8.tar.gz 6.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for redis-entities 1.0.8
File Interpreter ABI Platform
redis_entities-1.0.8-py3-none-any.whl Python 3 none any Details

Total release size: 16.0 kB

Release files / redis_entities-1.0.8.tar.gz

Download URL redis_entities-1.0.8.tar.gz
Size 6.5 kB
Tags Source
SHA-256 checksum
How to use checksums
1f424db079ac02909bacdc555482b8742f35a053082924da5438c04547910c2a
BLAKE2b-256 checksum
How to use checksums
a77ed3dba2735ebf446eb628edf661d130791805803aeb42c050aa133fe1e995
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.0 CPython/3.10.4

Release files / redis_entities-1.0.8-py3-none-any.whl

Download URL redis_entities-1.0.8-py3-none-any.whl
Size 9.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
1a542d72cc79ea548482e55efb62be3b14cc8eb1691bc4e93efecb0c60653184
BLAKE2b-256 checksum
How to use checksums
6ae894ea61d54bf613580f98b39069f6250549a4edb4510acca22724ec2f4cb5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.0 CPython/3.10.4

Release history Release notifications | RSS feed

This release

1.0.8 This release

2 release files

1.0.7

2 release files

1.0.6

2 release files

1.0.5

2 release files

1.0.4

2 release files

1.0.3

2 release files

1.0.2

3 release files

1.0.1

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page