redis

Ubuntu

1
sudo apt update && sudo apt install redis-server -y
1
2
$ ps ax | grep redis
1083463 ?        Ssl    0:00 /usr/bin/redis-server 127.0.0.1:6379

redis-cli

redis-cliコマンドでデータベース1に接続するには、次のように入力します。¹²

redis-cli -n 1

このコマンドは、データベース1に接続するためのものです。-nオプションを使用して、データベース番号を指定します。

上記の例では、データベース番号が1であることを示しています。

ソース: Bing との会話 2023/5/5

Django

1
poetry add django-redis

settings.py:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
CACHE_MIDDLEWARE_ALIAS = "default"  
CACHE_MIDDLEWARE_SECONDS = 600  
CACHE_MIDDLEWARE_KEY_PREFIX = ""
CACHES = {
    "default": {
        "BACKEND": "django_redis.cache.RedisCache",
        "LOCATION": "redis://127.0.0.1:6379/1",
        "OPTIONS": {
            "CLIENT_CLASS": "django_redis.client.DefaultClient",
        },
    }
}
1
2
3
4
from django.core.cache import cache

cache.set('key', 'value', timeout=600)
print(cache.get('key'))
1
2
3
4
$ redis-cli -n 1

127.0.0.1:6379[1]> keys *
1) ":1:key"
1
2
3
4
5
from django_redis import get_redis_connection

redis_conn = get_redis_connection("default")
keys = redis_conn.keys("*")
print(keys)
[b':1:key']

pickle形式

1
2
3
4
from django_redis import get_redis_connection

redis_conn = get_redis_connection("default")
keys = redis_conn.keys("*")
[b':1:workgroups']
1
2
3
import pickle
data = pickle.loads(redis_conn.get(b':1:workgroups'))
assert type(data) == list