Giter Site home page Giter Site logo

Comments (6)

maheshgawali avatar maheshgawali commented on July 20, 2024 1

I am facing this issue as well, but for the DefaultClient . I can submit a PR for it. Please let me know if such a feature is good to have or not @niwinz

I have a usecase where incr is intended to be done by mulitple processes and using set (to init keys) poses a risk of multiple processes NOT finding the key and then setting it to a default value and then incrementing.

value = redis_conn.get(redis_key)
if value:
    redis_conn.incr(redis_key, delta=count)
else:
    redis_conn.set(redis_key, delta=count, timeout=PREDECIDED_TIMEOUT)

what I want is:

redis_conn.incr(redis_key, delta=count)
redis_conn.expire(redis_key, timeout=PREDECIDED_TIMEOUT)

I have already forked and modified the code which works for me.

@niwinz : thanks for this great lib, it has been lot of help over the years.

from django-redis.

dennisai avatar dennisai commented on July 20, 2024

Typically, I would assume you can drop down to _client using get_cache._client, but apparently there is no attribute client for this cache.

from django-redis.

niwinz avatar niwinz commented on July 20, 2024

Hello!

What you mention is interesting to have, but it is not possible in my opinion. The main reason is that all data stored on redis, are serialized with pickle.

And this is one reason: to use the incr command of "redis" the value should be flat / native (not serialized).

Why serialized all? Because redistribution does not give me the type of data when redis return it. It always returns an "str"​​, then we have a problem when a chain store "123", it is unknown if a number or a string.

If I save on redis 123 (integer), redis will return "123" (string). With this problem of data typing of redis, I have to serialize all data so that the cache is always consistent.

Example::

>>> import redis
>>> r = redis.Redis()
>>> r.set('j', 1)
True
>>> a = r.get('j')
>>> print a, type(a)
1 <type 'str'>

And this is important in order to comply with the standard django cache backends, such as for applications that rely on the cache will always return the data type had previously saved.

In my opinion, for use incr, use directly a redis client.

from django-redis.

dennisai avatar dennisai commented on July 20, 2024

Okay, so how do I drop into client from a RedisCache instance? I get the following when I try to access _client:

AttributeError: 'ShardedRedisCache' object has no attribute '_client'

from django-redis.

niwinz avatar niwinz commented on July 20, 2024

ShardedRedisCache backendis is quite special, because the client is obtained from a redis hashring, and that depends on the key.

No single client instance of redis, there are many clients, as many as nodes configured in the shard.

You can obtain a client depending of key, example:

from django.core.cache import cache
redis_client = cache.get_server('some_key')

But be careful, this client is associated with that key, if you store another key with this client, it is likely that when you want to get the value with the api cache, you might not get the expected results.

from django-redis.

niwinz avatar niwinz commented on July 20, 2024

@maheshgawali if you have an improvement, please, submit it and we'll discus then over the code :D
if it make sense I'll gladly merge it \o/

from django-redis.

Related Issues (20)

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.