Comments (6)
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.
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.
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.
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.
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.
@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)
- Connection Pooling doesn't work as intended HOT 1
- Pytest 7.0 compatibility HOT 1
- Drop django 4.0
- Drop support for django 3.2 HOT 1
- How to configure and connect redis cluster correctly? HOT 2
- Django `TIMEOUT` setting not working as expected HOT 2
- Which timezone wil it take? system timezone or django timezone HOT 1
- Why Django Redis does not raise exception while connecting to Redis Server with SSL param equal string "False"
- `Expire` method fails when using `DEFAULT_TIMEOUT` HOT 1
- Getting/Setting occasionally extremely slow HOT 5
- django requirements update
- Add NX, XX, GT and LT to expire and pexpire HOT 2
- Multiple redis caches with fallback mechanism HOT 1
- Is it necessary to get coverage of tests directory? HOT 2
- Implementing HGET
- using redis in django getting connection error HOT 1
- strict_map_key
- Connection refused on Celery health checks HOT 3
- how to support hash ops, such as hset, hget...
- Reauthentication with Azure AD / Entra ID
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from django-redis.