💡

Redis(リディス)

に公開

Redisとは

https://chatgpt.com/share/681b4896-c5f4-800e-9b7f-1a8f4fd554c6

Redisを開発時使用する方法

https://chatgpt.com/share/681eb2d7-a94c-800e-9195-68ca67105558

扱える型

https://chatgpt.com/share/681ec1e5-9aec-800e-9f6c-5c0f7f6b11b0

Redis型 使うコマンド例 用途例
String SET key value 基本のキーと値
Hash HSET key field value オブジェクトっぽいデータ
List LPUSH key value キュー・スタック
Set SADD key value 重複なしの集合
Sorted Set ZADD key score value 順序付きの集合
Bitmap SETBIT key offset value ビット管理
HyperLogLog PFADD key value 概算ユニーク数カウント
Stream XADD key * field value イベントログ

SET(GET)

設定
SET mykey "Hello, World!"
取得
GET mykey
結果
"Hello, World!"
https://chatgpt.com/share/681ec1e5-9aec-800e-9f6c-5c0f7f6b11b0
https://chatgpt.com/share/681ff675-72cc-800e-b712-9943ad32a415
https://chatgpt.com/share/681ff6dc-a324-800e-98b2-8d9fcf5d1996
https://chatgpt.com/share/68200740-8b3c-800e-881e-bdfc7dae14e5
https://chatgpt.com/share/6820346d-cb58-800e-b7d0-2d16d4fbce20
https://chatgpt.com/share/6820367e-bd60-800e-9856-aa729a0d5d8c
https://chatgpt.com/share/682036d4-b1fc-800e-899a-5c10ec412b6c
https://chatgpt.com/share/68203851-999c-800e-9939-bfa4c79503b6

使用用途

① セッション情報の管理
Webアプリケーションでは、ユーザーがログインしたときのセッションIDを一時的に保存する用途。
② キャッシュの保存
データベースのクエリ結果や外部APIの結果を一時的にキャッシュする。
③ 一時的なフラグ管理
処理中・完了などの状態を一時的に記録する。
④ レートリミット・API制限
一定時間内のリクエスト数を制限するカウンターと組み合わせる。
⑤ ロック機構(分散ロック)
同じリソースに複数の処理がアクセスしないよう一時ロックする。

HSET(HGET、HGETALL、HDEL、HEXISTS、HLEN、HINCRBY、HINCRBYFLOAT)

1つのキーに対して複数のフィールドと値のペア(連想配列のような構造)」を保持できるデータ構造です。主に、ユーザー情報や設定情報のような構造化されたデータを格納するのに使われます。
フィールド数が少なく(数百以下)、かつ短い文字列なら非常にメモリ効率が良いです。
https://chatgpt.com/share/68227b9d-0b8c-800e-a84b-9aa2051a1e19

SADD(SREM、SMEMBERS、SISMEMBER、SCARD、SSCAN 、SINTER、SDIFF、SUNION、SINTERSTORE)

重複なしの集合のコマンド
https://chatgpt.com/share/68248109-50cc-800e-b70c-a87a1150cf22
https://chatgpt.com/share/6825168e-2f20-800e-9f7c-69a3578a3556
https://chatgpt.com/share/68251721-4d9c-800e-a260-c8ac8e1b1607
https://chatgpt.com/share/6825194e-3220-800e-a618-8fa2691fd41f
https://chatgpt.com/share/682519c2-fad0-800e-a2af-bc1348570fe8
https://chatgpt.com/share/68251b1b-f9e4-800e-a539-5977b61514c4

ZADD(ZSCORE、ZREM、ZCOUNT、ZCARD、ZPOPMIN、ZPOPMAX、ZINCRBY、ZRANGE)

順序付きの集合のコマンド
https://chatgpt.com/share/6825ceb5-3470-800e-909d-ae64d4644ddd
https://chatgpt.com/share/6825cebe-1020-800e-8517-c7e707be6d0a
https://chatgpt.com/share/6825cefd-8de4-800e-8f31-f486daee523a
https://chatgpt.com/share/6825cf9d-fd90-800e-bc8c-3d545f4a1449
https://chatgpt.com/share/6825cffd-b0dc-800e-93a5-cc6878329d9b
https://chatgpt.com/share/6825d08e-9314-800e-92fd-8274d86a1ead
https://chatgpt.com/share/6825d11d-4994-800e-a83d-0d5f680e527e
https://chatgpt.com/share/6825d1d8-96cc-800e-b868-fbe5ad008947

LPUSH(RPUSH、LPOP、RPOP、LRANGE、LLEN、LREM、LINDEX、LSET、LTRIM、BLPOP、BRPOP、BRPOPLPUSH、LPOS、LINSERT)

List型 に要素を「先頭から」追加するコマンドです。
https://chatgpt.com/share/6827e8be-240c-800e-b9aa-fddc805d3250
https://chatgpt.com/share/6827f7e8-6a2c-800e-8bec-485103713249
https://chatgpt.com/share/6827f8cb-bb94-800e-8c9a-373f7a586265
https://chatgpt.com/share/6827f942-dcb4-800e-a921-e354e705b90e

SORT

SORT コマンドは、リスト(list)、セット(set)、ソート済みセット(sorted set ではない)内の要素を並び替えるために使用されます。ただし、単なる並び替えにとどまらず、外部キーを参照したり、結果をLIMITしたり、取り出した結果を保存することもできます。
https://chatgpt.com/share/6827e87c-e7d0-800e-a40b-4b73e759397a

HyperLogLog(ハイパーログログ)

「概算でユニークな要素の数(基数)を数えるためのデータ構造」です。大量のデータを処理する場面で、「正確性よりもメモリ効率とスピードが重要」という場合に非常に役立ちます。

特徴 説明
目的 ユニークな値(例:IPアドレス、ユーザーIDなど)の数を概算する
精度 標準誤差約 0.81%(小さいが完全な正確性はない)
メモリ使用量 常に12KB程度(非常に小さい)
用途例 アクセス解析、ユニークユーザー数(UU)の計測、キャンペーン応募数など
使用するコマンドは
PFADD
PFCOUNT
PFMERGE
など

SETRANGE

Redis の文字列の 指定した位置から文字列を書き換える コマンド
https://chatgpt.com/share/682048e4-c980-800e-a50e-9217a09ff3cd

GETRANGE

Redis に保存されている文字列の 一部を取得するコマンド
https://chatgpt.com/share/68204959-af28-800e-8231-e87bfd8fb1a2

INCRとDECR

数値のインクリメント(加算)とデクリメント(減算)コマンド
https://chatgpt.com/share/6821cf74-f568-800e-a2e4-42064516d287

INCRBYとDECRBY

指定した数だけ増減するコマンド
https://chatgpt.com/share/6821d084-0160-800e-9e37-c3a564d8cb14

INCRBYFLOAT

動小数点数の加算減算するコマンド
https://chatgpt.com/share/6821d0f8-a2b0-800e-9b98-f3ec73727edf

DEL

DEL キー
キーの指定は複数OK
https://chatgpt.com/share/682039e7-d33c-800e-84f9-e57df5d03962

UNLINKとの使い分け

DELは、非同期ではなく同期的に即時実行。
UNLINKは、非同期に削除したい場合に使用します(パフォーマンス重視)。

特定のページをキャッシュするサンプル

https://chatgpt.com/share/6822785b-7b68-800e-ac57-00804c69aca7

セッションを設定取得するサンプル

https://chatgpt.com/share/6823c702-3e30-800e-b63f-4263f17f9814

登録した情報のページを取得する際のサンプル

https://chatgpt.com/share/6823ca85-2898-800e-9ea7-874d12d7d804

いいねのサンプル

https://chatgpt.com/share/682522ff-2edc-800e-aef8-44ea27a24acb

Discussion