Code Monkey home page Code Monkey logo

serialized-redis's Introduction

serialized-redis

Redis python interface that serializes all values using json, pickle, msgpack or a custom serializer.

image

image

Getting Started

Installation

pip install serialized-redis-interface

Usage

>>> import serialized_redis

>>> r = serialized_redis.JSONSerializedRedis(host='localhost', port=6379, db=0)
>>> r.set('foo', { 'test': 'dict' })
True
>>> r.get('foo')
{'test': 'dict'}

>>> r = serialized_redis.PickleSerializedRedis(host='localhost', port=6379, db=0)
>>> r.set('foo', { 'test': 'dict' })
True
>>> r.get('foo')
{'test': 'dict'}

>>> r = serialized_redis.MsgpackSerializedRedis(host='localhost', port=6379, db=0)
>>> r.set('foo', { 'test': 'dict' })
True
>>> r.get('foo')
{'test': 'dict'}

serialized-redis extends redis-py and uses the same interface.

Most commands, Piplines and PubSub are supported and take care of serializing and deserializing values.

msgpack must be installed in order to use MsgpackSerializedRedis.

All strings are python str.

Limitations

As values are serialized, Redis operations that manipulate or extract data from values are not supported.

  • SORT commands may not return correct order depending on the serializer used.
  • ZSCAN and SSCAN MATCH option will only work for exact match.
  • STRLENGTH and HSTRLENGTH will return the length of the serialized value.
  • all lexicographical commands like ZLEXCOUNT, ZREMRANGEBYLEX and ZREVRANGEBYLEX are not supported
  • INCR is only supported with JSON serializer
  • fields of Redis hashes are not serialized

Extra Methods

  • smembers_as_list, sdiff_as_list, sinter_as_list, sunion_as_list can be used when members of the redis set may not be hashable once deserialized.

    >>> r = serialized_redis.JSONSerializedRedis() 
    >>> r.sadd('myset', {'dict': 1})
    1
    >>> r.smembers('myset')
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "/home/michael/workspace/Origin_Nexus/serialized_redis/serialized_redis/__init__.py", line 176, in smembers
        return set(super().smembers(*args, **kwargs))
    TypeError: unhashable type: 'dict'
    >>> r.smembers_as_list('myset')
    [{'dict': 1}]
  • smart_get and smart_set can be used to retrieve and store python structure with their redis counterpart:
    • python list as redis LIST
    • python set as redis SET
    • python dict as redis HASH, fields will not be (de)serialized.

Custom Serializer

You can use your own seriliazing and deserializing functions:

>>> r = serialized_redis.SerializedRedis(serialization_fn=my_serializer, deserialization_fn=my_deserializer)

If your deserializer function expects python 3 strings instead of bytes, you can add decode_responses=True parameter.

Decoding bytes to str when required is the responsability of the deserialization function.

serialized-redis's People

Contributors

michael-mri avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

serialized-redis's Issues

redis-py 3.0 compatibility

Thank you for developing such a great module.

Redis-py 3.0 has made some backward incompatible changes and added many new features.

Currently serialized-redis is not compatible with redis-py 3.0.

Is there any upgrade timeline?

Returns as dict instead of orderdict

Hey! I hope there is still someone that could help me out here!

I have seen that there is some missing parts regarding the msgpack which leads my problem where when I store to redis, its actually does not do the orderdict anymore but instead returns as dict.

After looking at the code I have seen there is a missing part where I do not see that you store as a msgpack.pack :

image

and also using dumps.

My problem with this is that whenever I store to Redis using serialized-redis, for example store headers from a requests. It stores it but it doesn't store it the same RAW headers orders as it was from the response but instead changes which causing me to have an incorrect orderdict.

I do hope there is a chance someone could help me out to get it fixed :) Looking forward for reply!

Doesn't work

import serialized_redis

r = serialized_redis.JSONSerializedRedis(host='localhost', port=6379, db=0)

python test_redis_connection.py

Traceback (most recent call last):
File "/Users/ivanfigueredo/Downloads/volworks/python/amplify/test_redis_connection.py", line 2, in
from redis_connection import connect_redis
File "/Users/ivanfigueredo/Downloads/volworks/python/amplify/redis_connection.py", line 1, in
import serialized_redis
File "/opt/miniconda3/envs/dash_apps_36/lib/python3.10/site-packages/serialized_redis/init.py", line 5, in
from redis.client import string_keys_to_dict, dict_merge
ImportError: cannot import name 'dict_merge' from 'redis.client' (/opt/miniconda3/envs/dash_apps_36/lib/python3.10/site-packages/redis/client.py)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.