GithubHelp home page GithubHelp logo

Comments (4)

AIGeneratedUsername avatar AIGeneratedUsername commented on June 20, 2024

Please format your example with Markdown because now your message is unreadable. See Creating and highlighting code blocks.

Please include:

  • cashews version
  • redis package version (pip show redis)
  • error traceback

from cashews.

Krukov avatar Krukov commented on June 20, 2024

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.

Krukov avatar Krukov commented on June 20, 2024

Do not use returnings of setup

from cashews.

xatsuriyo avatar xatsuriyo commented on June 20, 2024

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)

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.