Comments (4)
Please format your example with Markdown because now your message is unreadable. See Creating and highlighting code blocks.
Please include:
cashews
versionredis
package version (pip show redis
)- error traceback
from cashews.
Hi @daniskazan
The problem is how you use cashews
redis_client = cashews.cache.setup(settings_url=REDIS_URL,db=1,wait_for_connection_timeout=0.5,safe=False,enable=True)
That is not a right way and there are no examples with a code like that
Take a loot at readme
https://github.com/Krukov/cashews#simple-cache
https://github.com/Krukov/cashews#basic-api
from cashews.
Do not use returnings of setup
from cashews.
I also have this error when using redis,
in-memory cache are working perfectly fine
AttributeError: 'Redis' object has no attribute '_client'
its my first time using cache manager, i need to speed up some variable load than directly using my mysql connections.
Traceback (most recent call last):
File "/home/rietzee/KEK-CID/class/db/cashew_cache/cashew_manager.py", line 121, in <module>
asyncio.run(run())
File "/usr/lib/python3.11/asyncio/runners.py", line 190, in run
return runner.run(main)
^^^^^^^^^^^^^^^^
File "/usr/lib/python3.11/asyncio/runners.py", line 118, in run
return self._loop.run_until_complete(task)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/lib/python3.11/asyncio/base_events.py", line 653, in run_until_complete
return future.result()
^^^^^^^^^^^^^^^
File "/home/rietzee/KEK-CID/class/db/cashew_cache/cashew_manager.py", line 105, in run
get_cache_lock = await CCM.getlock_cache(Cache, "test")
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/rietzee/KEK-CID/class/db/cashew_cache/cashew_manager.py", line 55, in getlock_cache
if not await self.is_cache_locked(Cache, key_name):
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/rietzee/KEK-CID/class/db/cashew_cache/cashew_manager.py", line 46, in is_cache_locked
return await Cache.is_locked(key_name)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/rietzee/KEK-CID/pyenv/lib/python3.11/site-packages/cashews/backends/redis/backend.py", line 138, in is_locked
return await self.exists(key)
^^^^^^^^^^^^^^^^^^^^^^
File "/home/rietzee/KEK-CID/pyenv/lib/python3.11/site-packages/cashews/backends/redis/backend.py", line 158, in exists
return bool(await self._client.exists(key))
^^^^^^^^^^^^
AttributeError: 'Redis' object has no attribute '_client'
connecting using redis code:
import asyncio
from decimal import Decimal
from datetime import datetime
import logging
from cashews import Command, add_prefix, all_keys_lower, cache # noqa: F401
class Cashew_Cache_Manager:
async def connect_cache_data(self):
redis_url = "redis://localhost"
Cache = cache.setup(settings_url=redis_url,db=1,wait_for_connection_timeout=0.5,safe=False,enable=True)
#Cache = cache.setup("mem://")
return Cache
# Check Lock & Get Lock
# check is cache locked
async def is_cache_locked(self, Cache, key_name):
return await Cache.is_locked(key_name)
#return Cache.is_locked(key_name)
# Get Lock
async def getlock_cache(self, Cache, key_name):
# Check Lock
check_lock = True
while check_lock:
lock = Cache.lock(key_name, expire=60, wait=True)
if not await self.is_cache_locked(Cache, key_name):
check_lock = False
return lock
# Release Lock
async def releaselock_cache(self, Cache, lock):
return await Cache.unlock(lock, value=True)
if __name__ == "__main__":
import asyncio
async def run():
CCM = Cashew_Cache_Manager()
Cache = await CCM.connect_cache_data()
print("Cache", Cache)
get_cache_lock = await CCM.getlock_cache(Cache, "test")
print("get_cache_lock", get_cache_lock)
asyncio.run(run())
this code running perfectly fine using in-memory cache
Cache <cashews.backends.memory.Memory object at 0x7f36ea7d15c0>
get_cache_lock <contextlib._AsyncGeneratorContextManager object at 0x7f36ea483410>
Code running with in-memory cache
import asyncio
from decimal import Decimal
from datetime import datetime
import logging
from cashews import Command, add_prefix, all_keys_lower, cache # noqa: F401
class Cashew_Cache_Manager:
async def connect_cache_data(self):
#redis_url = "redis://localhost"
#Cache = cache.setup(settings_url=redis_url,db=1,wait_for_connection_timeout=0.5,safe=False,enable=True)
Cache = cache.setup("mem://")
return Cache
# Check Lock & Get Lock
# check is cache locked
async def is_cache_locked(self, Cache, key_name):
return await Cache.is_locked(key_name)
#return Cache.is_locked(key_name)
# Get Lock
async def getlock_cache(self, Cache, key_name):
# Check Lock
check_lock = True
while check_lock:
lock = Cache.lock(key_name, expire=60, wait=True)
if not await self.is_cache_locked(Cache, key_name):
check_lock = False
return lock
# Release Lock
async def releaselock_cache(self, Cache, lock):
return await Cache.unlock(lock, value=True)
if __name__ == "__main__":
import asyncio
async def run():
CCM = Cashew_Cache_Manager()
Cache = await CCM.connect_cache_data()
print("Cache", Cache)
get_cache_lock = await CCM.getlock_cache(Cache, "test")
print("get_cache_lock", get_cache_lock)
asyncio.run(run())
from cashews.
Related Issues (20)
- CacheEtagMiddleware breaks with redis client_side cache HOT 1
- Counter in Cache condition
- cache_control_ttl is undocumented HOT 1
- Documenting with sphinx? HOT 1
- ValueError: Duplicated timeseries in CollectorRegistry HOT 2
- early_ttl not working in combination with CacheEtagMiddleware HOT 5
- Gracefully close a connection pool with redis > 5.0.1
- Cannot close Redis connection - AttributeError: 'Redis' object has no attribute '_client' HOT 4
- Cache doesn't work with GZIPMiddleware HOT 5
- JSON serializer HOT 5
- Early cache with sqlalchemy & fastapi depend (sqlalchemy.exc.IllegalStateChangeError: Method 'close()' can't be called here; method '_connection_for_bind()' is already in progress and this would cause an unexpected state change to <SessionTransactionState.CLOSED: 5>) HOT 1
- Formatter Called with Formatted Value HOT 6
- CacheRequestControlMiddleware: Backend not found exception with non-default cache prefix HOT 3
- cache_control_ttl can't be used directly with cache api HOT 1
- thunder_protection with multiple event loops breaks HOT 3
- re.error: bad character in group name when using custom formatter in a tag HOT 3
- Unhandled exception when there are only backends with prefixes HOT 1
- Feature: Get or set
- _empty and _empty_in_redis marker objects leak HOT 1
- Redis Client Side Connection Timeout HOT 3
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 cashews.