2 ๋ถ„ ์†Œ์š”

Redis ์„ค์ •ํ•˜๊ธฐ

settings.py ์— ์•„๋ž˜์™€ ๊ฐ™์ด Redis ์„ค์ •์„ ์ถ”๊ฐ€ํ•œ๋‹ค.

CACHES = {
    "default": {
        "BACKEND": "django_redis.cache.RedisCache",
        "LOCATION": "redis://127.0.0.1:6379",
        "OPTIONS": {
            "CLIENT_CLASS": "django_redis.client.DefaultClient",
        },
        "TIMEOUT": 300,  # ์บ์‹œ ๊ธฐ๋ณธ ์œ ํšจ ์‹œ๊ฐ„
    }
}

RedisCache ๋ฅผ ์‚ฌ์šฉํ•˜๊ณ , ์บ์‹œ์— ์ €์žฅ๋˜๋Š” ๊ฐ’์€ 5๋ถ„(300์ดˆ)์˜ ํƒ€์ž„์•„์›ƒ ์„ ๋ถ€์—ฌํ•จ.

์‹คํ–‰ ๋ฐฉ๋ฒ•

$ python manage.py runserver

์ด๋Ÿฌ๋ฉด ์„œ๋ฒ„๊ฐ€ ์‹คํ–‰๋˜๊ณ ..

๋จผ์ € Redis ๋ฅผ ๋‹ค์šด๋ฐ›๊ณ , Django์—์„œ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด Django-Redis ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์„ค์น˜ํ•จ.

$ pip install django-redis

๋˜ ๋‹ค๋ฅธ ํ„ฐ๋ฏธ๋„์„ ์—ด์–ด์„œ Redis ๋ฅผ ์—ด์–ด์ค€๋‹ค.

์•„๋ž˜๋Š” Windows ๊ธฐ์ค€

redis-server ์—ด๊ธฐ

[Redis ์„ค์น˜ ๊ฒฝ๋กœ]$ .\redis-server.exe

[20748] 08 Jan 18:26:12.431 # Warning: no config file specified, using the default config. In order to specify a config file use C:\Users\Aptimizer\Redis-x64-3.0.504\redis-server.exe /path/to/redis.conf
                _._
           _.-``__ ''-._
      _.-``    `.  `_.  ''-._           Redis 3.0.504 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 20748
  `-._    `-._  `-./  _.-'    _.-'
 |`-._`-._    `-.__.-'    _.-'_.-'|
 |    `-._`-._        _.-'_.-'    |           http://redis.io
  `-._    `-._`-.__.-'_.-'    _.-'
 |`-._`-._    `-.__.-'    _.-'_.-'|
 |    `-._`-._        _.-'_.-'    |
  `-._    `-._`-.__.-'_.-'    _.-'
      `-._    `-.__.-'    _.-'
          `-._        _.-'
              `-.__.-'

[20748] 08 Jan 18:26:12.434 # Server started, Redis version 3.0.504
[20748] 08 Jan 18:26:12.434 * DB loaded from disk: 0.000 seconds
[20748] 08 Jan 18:26:12.434 * The server is now ready to accept connections on port 6379

redis-cli ์—ด๊ธฐ

[Redis ์„ค์น˜ ๊ฒฝ๋กœ]$ .\redis-cli.exe -h 127.0.0.1 6379

127.0.0.1:6379> 
  • redis-cli ์—์„œ ์บ์‹œ๋œ ๋ฐ์ดํ„ฐ๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Œ

์ด ์ •๋„๋งŒ ์—ด์–ด๋†“์œผ๋ฉด, ์ด๋ฉ”์ผ ์ธ์ฆ ์ฝ”๋“œ ๋ฐœ์†ก ๊ธฐ๋Šฅ์€ ์ •์ƒ์ ์œผ๋กœ ๋™์ž‘ํ•จ

์ธ์ฆ ์ฝ”๋“œ ํ™•์ธ ๋ฐฉ๋ฒ•?

์™œ Redis๋ฅผ ์‚ฌ์šฉํ• ๊นŒ?

  • ์ด๋ฉ”์ผ ์ธ์ฆ ์ฝ”๋“œ๋Š” ์งง์€ ์œ ํšจ ๊ธฐ๊ฐ„ ๋™์•ˆ๋งŒ ํ•„์š”ํ•˜๋‹ค๋Š” ๋ถ€๋ถ„
    • ์ด๋Ÿฐ ์ž„์‹œ ๋ฐ์ดํ„ฐ๋ฅผ DB์— ์ €์žฅํ•˜๋Š” ๊ฒƒ์€ ๋น„ํšจ์œจ์ 
    • Redis์™€ ๊ฐ™์€ ์ธ๋ฉ”๋ชจ๋ฆฌ DB ์— ์ €์žฅํ•˜๋Š” ๊ฒƒ์ด ํšจ์œจ์ 
  • ๋ฐ์ดํ„ฐ๊ฐ€ ๋งŒ๋ฃŒ๋˜๋ฉด ์ž๋™์œผ๋กœ ์‚ญ์ œ(EXPIRE ์„ค์ •)๋˜๋ฏ€๋กœ, ์ถ”๊ฐ€์ ์ธ ๋ฐ์ดํ„ฐ ์ •๋ฆฌ๊ฐ€ ํ•„์š” ์—†๋‹ค๋Š” ์žฅ์ 
  • ๊ณ ์† ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ
    • Redis๋Š” ์ธ๋ฉ”๋ชจ๋ฆฌ DB๋ผ์„œ ์ผ๋ฐ˜ DB๋ณด๋‹ค ์†๋„๊ฐ€ ํ›จ์”ฌ ๋น ๋ฆ„
    • ์ด๋ฉ”์ผ ์ธ์ฆ์€ ๋น ๋ฅธ ์†๋„๊ฐ€ ์ค‘์š”ํ•˜๊ธฐ ๋•Œ๋ฌธ โ†’ ์—ฌ๋Ÿฌ ์‚ฌ์šฉ์ž๊ฐ€ ๋™์‹œ์— ์ธ์ฆ ์ฝ”๋“œ๋ฅผ ์š”์ฒญํ•˜๋Š” ๊ฒฝ์šฐ..
  • DB ๋ถ€๋‹ด ๊ฐ์†Œ
    • Redis๋Š” <KEY:VALUE> ํ˜•์‹์„ ์‚ฌ์šฉํ•˜๊ธฐ ๋•Œ๋ฌธ์—, ๋ณ„๋„์˜ DB ํ…Œ์ด๋ธ” ์„ค๊ณ„ ํ•„์š” ์—†์Œ
    • ์ธ์ฆ ์ฝ”๋“œ์™€ ์ด๋ฉ”์ผ๋งŒ ์ €์žฅ โ†’ ๋ถ€๋‹ด ๊ฐ์†Œ

ํƒœ๊ทธ:

์นดํ…Œ๊ณ ๋ฆฌ:

์—…๋ฐ์ดํŠธ:

๋Œ“๊ธ€๋‚จ๊ธฐ๊ธฐ