【问题标题】:Get list of Cache Keys in Django获取 Django 中的缓存键列表
【发布时间】:2012-02-21 07:48:39
【问题描述】:

我试图了解 Django 如何为我的视图设置键。我想知道是否有办法从 Memcached 获取所有已保存的密钥。像cache.all() 之类的东西。我一直在尝试使用cache.has_key('test') 查找密钥,但仍然无法弄清楚视图密钥是如何命名的。

更新:我需要这个的原因是因为我需要手动删除部分缓存但不知道 Django 为我的 cache_view 键设置的键值

【问题讨论】:

标签: python django caching memcached


【解决方案1】:

对于RedisCache,您可以获得所有可用的密钥。

from django.core.cache import cache

cache.keys('*')

【讨论】:

  • 'RedisCache' 对象没有属性 'keys'
  • @KaramHaj 我认为对于较新的版本,您可以尝试使用cache.get('*')
  • @KaramHaj cache.get('*') 返回 None 而不是像 cache.keys('*') 这样的每个键。在 django 3.2.3 和 redis 3.5.3 上测试
【解决方案2】:

如前所述,无法在 django 中获取所有缓存键的列表。如果您使用的是外部缓存(例如 memcached 或数据库缓存),您可以直接检查外部缓存。

但如果你想知道如何将一个 django 密钥转换为后端系统中使用的密钥,django 的 ma​​ke_key() 函数会做到这一点。

https://docs.djangoproject.com/en/1.8/topics/cache/#cache-key-transformation

>>> from django.core.cache import caches
>>> caches['default'].make_key('test-key')
u':1:test-key'

【讨论】:

    【解决方案3】:
    【解决方案4】:

    切换到使用 LocMemCache 而不是 MemcachedCache:

    CACHES = {
        'default': {
            'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
            'LOCATION': 'unique-snowflake',
        }
    }
    

    然后看问题Contents of locmem cache in Django?

    from django.core.cache.backends import locmem
    print(locmem._caches)
    

    【讨论】:

      【解决方案5】:

      您可以使用来自:https://github.com/dlrust/python-memcached-stats 的 memcached_stats。这个包使得在 python 环境中查看 memcached 的键成为可能。

      【讨论】:

        【解决方案6】:

        如果这不是太过时,我有类似的问题,因为我不得不遍历整个缓存。我管理它,当我在我的缓存中添加一些东西时,就像下面的伪代码:

        #create caches key list if not exists
        if not my_cache.get("keys"):
            my_cache.set("keys", [])
        
        #add to my cache
        my_cache.set(key, value)
        
        #add key to keys
        if key not in my_cache.get("keys"):
            keys_list = my_cache.get("keys")
            keys_list.append(key)
            my_cache.set("keys", keys_list)
        

        【讨论】:

          【解决方案7】:

          Memcached documentation recommends 不是列出所有缓存键,而是在详细模式下运行 memcached 并查看所有更改。你应该像这样启动 memcached

          memcached -vv
          

          然后它会在创建/更新/删除密钥时打印它们。

          【讨论】:

            【解决方案8】:

            这有帮助。

            参考:

            https://lzone.de/blog/How-to%20Dump%20Keys%20from%20Memcache

            https://github.com/dlrust/python-memcached-stats

            import re, telnetlib, sys
            
            key_regex = re.compile(r"ITEM (.*) \[(.*); (.*)\]")
            slab_regex = re.compile(r'STAT items:(.*):number')
            
            class MemcachedStats:
            
                def __init__(self, host='localhost', port='11211'):
                    self._host = host
                    self._port = port
                    self._client = None
            
                @property
                def client(self):
                    if self._client is None:
                        self._client = telnetlib.Telnet(self._host, self._port)
                    return self._client
            
                def command(self, cmd):
                    ' Write a command to telnet and return the response '
                    self.client.write("{}\n".format(cmd).encode())
                    res = self.client.read_until('END'.encode()).decode()
                    return res
            
                def slab_ids(self):
                    ' Return a list of slab ids in use '
                    slab_ids =  slab_regex.findall(self.command('stats items'))
                    slab_ids = list(set(slab_ids))
                    return slab_ids
            
                def get_keys_on_slab(self, slab_id, limit=1000000):
                    cmd = "stats cachedump {} {}".format(slab_id, limit)
                    cmd_output = self.command(cmd)
                    matches = key_regex.findall(cmd_output)
                    keys = set()
                    for match_line in matches:
                        keys.add(match_line[0])
                    return keys
            
                def get_all_keys(self):
                    slab_ids = self.slab_ids()
                    all_keys = set()
                    for slab_id in slab_ids:
                        all_keys.update(self.get_keys_on_slab(slab_id))
                    return list(all_keys)
            
            
            def main():
                m = MemcachedStats()
                print(m.get_all_keys())
            
            if __name__ == '__main__':
                main()
            

            【讨论】:

              【解决方案9】:

              在我的设置中,有一种方法可以为 Redis 获取“原始”客户端,您可以从中获取密钥。

              from django.core.cache import cache
              
              cache.get_client(1).keys()
              

              【讨论】:

                【解决方案10】:

                有一些奇怪的解决方法可以从命令行获取所有键,但是没有办法在 Django 中使用 memcached 来做到这一点。见this thread

                【讨论】:

                  猜你喜欢
                  • 1970-01-01
                  • 2017-12-17
                  • 2018-12-01
                  • 2018-04-18
                  • 1970-01-01
                  • 2015-10-25
                  • 1970-01-01
                  • 1970-01-01
                  • 2015-03-27
                  相关资源
                  最近更新 更多