00.redis入門
12064 ワード
[TOC]
redisって何?
公式解釈:redisはオープンソースで、メモリ内のK-Vデータ構造ストレージシステムであり、データベース、キャッシュ、メッセージミドルウェアとして使用できます.文字列(strings)、ハッシュ(hashes)、リスト(lists)、セット(sets)、順序付きセット(sorted sets)、範囲クエリー、bitmaps、hyperlogs、地理空間(geospatial)インデックス半径クエリーなど、さまざまなタイプのデータ構造をサポートします.redisにはレプリケーション(replication)、LUAスクリプト(Lua scripting)、LRU駆動イベント(LRU eviction)、トランザクション(transactions)、および異なるレベルのディスク持続化(persistence)、redis哨兵(Sentine)および自動パーティション(Cluster)による高可用性(highavailability)が内蔵されています.
redisはremote dictionary serverの略であり,Redisの位置付けを明らかに理解できる.redisは簡単に見るとメモリ(実行時)のhashtableであり、すべてのデータはメモリにあり、最も基礎的なputとget操作をサポートしてhashテーブル内のデータを修正します.これが最も基本的な機能で、メモリベースなので効率的な読み書き(公式に与えられたデータは10 w回/sの読み書き性能)をサポートし、最も一般的な使用シーンであるキャッシュを思い浮かべやすい.また、データの永続化(2つの永続化ポリシーがある)をサポートするため、nosqlのソリューションとしても使用できます.
ベースのデータアクセスに加えて、redisは分散をサポートするため、分散キャッシュと分散メッセージミドルウェア、および分散データベースを構築して、高可用性を提供することができます.
redisの主な特性速度が速い データはメモリに記憶する .キー値ペアに基づくデータ構造サーバ は、複数のデータ型 をサポートします.は、レプリケーション、HyperLogLog、トランザクション、サブスクリプションの発行、永続化など、豊富な機能を備えています. 単純安定 クライアントは大量の言語をサポートする は、高可用性および分散型 をサポートします.
シングルスレッドアーキテクチャ
Redisサービスは単一スレッドのプロセスであり,下位層はIO多重モデルを用いて高性能のメモリデータベースサービスをサポートする.
redisに送信されたすべてのコマンドは、1つのコマンドキューに入り、1つのコマンドが実行されるため、あるコマンドが長く実行されると、redis全体のパフォーマンスに影響します.
redisはどうしてこんなに速いの?
公式に与えられたredisの読み書き性能は数万~数十万回毎秒、How fast is Redis?具体的なパフォーマンスは、システムおよびハードウェアに関連しています.
redisの性能が良いのはいくつかの主な原因があります.メモリベース、およびディスクの読み書きは、1桁の ではありません. C言語というオペレーティングシステムに近い言語を使用して、 の実行性能が優れている.単一スレッドのアーキテクチャ設計、同時制御のオーバーヘッドがない redisソースコードは非常に優れている は、IO多重化技術としてepollを用いる、NIO(NonBlocking IO) を実現する.
Redisは単一スレッドモデルに基づいており,使用中はスレッドがブロックされていることに注意しなければならない.
redis使用シーンキャッシュ ランキングシステム カウンタアプリケーション ソーシャルネットワーク メッセージキュー redis環境構築
redisは多くのオペレーティングシステムの特性を使用しており、Linuxシステムに基づいており、公式にはWindowsシステムはサポートされていません.WindowsバージョンはWindowsチームが開発したブランチです.
ダウンロード
インストールに成功すると、redis関連実行ファイルは/usr/local/binにあります.
この中にはredis内に建てられたshellがあります.
redis shell redis-server redisのサービス側、役割はredisサービス を起動することである. redis-cliは、redisサービス操作redis に接続するためのredisのコマンドラインクライアントである. redis-benchmarkはredisの性能テストツール である. redis-check-aofは、RedisAOF永続化ファイル検出および修復ツール である. redis-check-rdbは、redis RDB永続化ファイルの検出および修復ツール である. redis-sentinelはredis哨兵機能モードであり、分布式 に用いられる.
起動と停止バインドポート番号 ログファイル redisの作業ディレクトリ deamonで を実行するかどうか
サービスの開始
サービスの接続と使用
redis-cliという内蔵cliツールを使用するか、公式に提供されている様々なプログラミング言語のapiを使用してredisサービスに接続します. redis cli接続 を使用
redis-cliには多くのパラメータがあり、インタラクティブ実行コマンドと非インタラクティブをサポートしています. redis-cliヘルプドキュメント: バックグラウンド起動
または、プロファイルでdaemonizeをyesに設定し、起動時にプロファイルを指定します.
使用方法からredisは全体的にC-Sアーキテクチャのコンポーネントであり,mysqlなどのDBとは差が少ないように見え,サービス側のデータコンテナとしてクライアントがネットワーク(実際にはTCPプロトコルに基づくRDBP)を介してserverに接続し,データまたはメタデータを操作するコマンドを実行する.
サービスの停止 redis-cli shutdown システムのkillコマンド(使用しない) redis-cli shutdownコマンドは友好的な停止コマンドであり、停止プロセスには3つの段階が含まれています.クライアントとの接続を切断する .永続化データ(save|nosaveオプションを指定して永続化するかどうかを選択できますが、デフォルトでは永続化されます) 終了 killコマンドを使用すると、プロセスが強制的に殺され、データは保存されません.
redisのK-Vデータ型
key
keyはstringタイプで、redisのhashtableのkeyです.
一般的な操作とコマンド keyが存在するか否かを判断する .設定可能値 可能なvalue を取得 keyのvalueを取得するタイプ keyの有効期限を設定します.単位は秒 です. keyの期限切れ時刻、UNIXタイムスタンプ、UTCから時刻までの秒数 を設定する. keyの有効期限を設定します.ミリ秒あたり です. keyの期限切れ設定をキャンセルし、直接持続化し、期限切れ時間は-1で、デフォルトはすべて持続的な です. keyの有効期限を表示します.単位秒 keyの有効期限を表示します.ミリ秒あたり ランダムに1つのkey を返す key名 の名前を変更 key名を新しいkey名に変更し、 が存在しないことを要求するマッチングモードのkey を返す削除key keyの数 テスト
value
valueには、次のようなデータ型があります. string(ベース) hash(stringタイプのfieldとvalueのマッピングテーブル、hashは特にオブジェクトを格納するのに適している) list(文字列リスト) set(無秩序で重複しない文字列の集合) Sorted set(整列集合) keyに対応するvalueのタイプは、コマンド
string
stringはredisの基礎データ型であり、keyのデータ型はstringであり、他の複合型のデータもstringに基づいている.stringは最大512 MBのデータを格納できる.
hash
すなわちHashMapはredisのインフラストラクチャと同様にkey-valueのデータ型である.hashのkeyとvalueはstringタイプ(hashのvalueがhashであるなどのネストされたデータ構造はサポートされていない)であり、hashのsizeは4バイト空間、すなわち32ビットのシンボル整形数の取値範囲である.
hashはjsonの簡略化されたフォーマットと同様にオブジェクトを記述するのに適している.データ型のネストはサポートされていないため、オブジェクトの属性がオブジェクトであるかというモデルを記述することはできない.同様のサポートのみ:
下位層は双方向チェーンテーブルで実現される線形データ構造であるため,ヘッダテールにノードを追加する効率は高いが,ランダムアクセス効率は劣る.容量空間も32ビットです.内部要素タイプはstringです.両端に要素を追加できます.
類似
redisのSetはstring型の無秩序集合であり,要素は重複せず,重複追加は直接無視される.集合はハッシュテーブルによって実現されるので,追加,削除,検索の複雑さはいずれもO(1)である.集合は、交差、並列、差分などを求める操作を提供します.
sorted set
Setの要素に重みパラメータscoreを追加し、要素をscore順に並べます.hashテーブルとskiplistで実現されているので,中間位置のデータを読み取る速度も速い(O(lgN)).
Skip Listはランダム化されたデータ構造であり、並列チェーンテーブルに基づいており、その効率は二叉ルックアップツリー(ほとんどの操作に対してO(log n)平均時間を必要とする)よりも優れている.基本的には,ジャンプリストは秩序化されたチェーンテーブルに付加された前進リンクであり,増加はランダム化された方法で行われるため,リスト内の検索は部分リストを迅速にスキップすることができる(従って名付けられた).すべての操作は対数的にランダム化された時間で行われる.Skip Listは,秩序チェーンテーブルが特定の値を検索する困難をうまく解決できる.
参考資料[1]Redisチュートリアル [2]李子骅.Redis入門ガイド [3]付磊、張益軍.Redis開発と運用メンテナンス
redisって何?
公式解釈:redisはオープンソースで、メモリ内のK-Vデータ構造ストレージシステムであり、データベース、キャッシュ、メッセージミドルウェアとして使用できます.文字列(strings)、ハッシュ(hashes)、リスト(lists)、セット(sets)、順序付きセット(sorted sets)、範囲クエリー、bitmaps、hyperlogs、地理空間(geospatial)インデックス半径クエリーなど、さまざまなタイプのデータ構造をサポートします.redisにはレプリケーション(replication)、LUAスクリプト(Lua scripting)、LRU駆動イベント(LRU eviction)、トランザクション(transactions)、および異なるレベルのディスク持続化(persistence)、redis哨兵(Sentine)および自動パーティション(Cluster)による高可用性(highavailability)が内蔵されています.
redisはremote dictionary serverの略であり,Redisの位置付けを明らかに理解できる.redisは簡単に見るとメモリ(実行時)のhashtableであり、すべてのデータはメモリにあり、最も基礎的なputとget操作をサポートしてhashテーブル内のデータを修正します.これが最も基本的な機能で、メモリベースなので効率的な読み書き(公式に与えられたデータは10 w回/sの読み書き性能)をサポートし、最も一般的な使用シーンであるキャッシュを思い浮かべやすい.また、データの永続化(2つの永続化ポリシーがある)をサポートするため、nosqlのソリューションとしても使用できます.
ベースのデータアクセスに加えて、redisは分散をサポートするため、分散キャッシュと分散メッセージミドルウェア、および分散データベースを構築して、高可用性を提供することができます.
redisの主な特性
シングルスレッドアーキテクチャ
Redisサービスは単一スレッドのプロセスであり,下位層はIO多重モデルを用いて高性能のメモリデータベースサービスをサポートする.
redisに送信されたすべてのコマンドは、1つのコマンドキューに入り、1つのコマンドが実行されるため、あるコマンドが長く実行されると、redis全体のパフォーマンスに影響します.
redisはどうしてこんなに速いの?
公式に与えられたredisの読み書き性能は数万~数十万回毎秒、How fast is Redis?具体的なパフォーマンスは、システムおよびハードウェアに関連しています.
redisの性能が良いのはいくつかの主な原因があります.
Redisは単一スレッドモデルに基づいており,使用中はスレッドがブロックされていることに注意しなければならない.
redis使用シーン
redisは多くのオペレーティングシステムの特性を使用しており、Linuxシステムに基づいており、公式にはWindowsシステムはサポートされていません.WindowsバージョンはWindowsチームが開発したブランチです.
ダウンロード
wget http://download.redis.io/releases/redis-4.0.10.tar.gz
tar xzf redis-4.0.10.tar.gz
cd redis-4.0.10
make && make install
インストールに成功すると、redis関連実行ファイルは/usr/local/binにあります.
[root@aliyun bin]# ll /usr/local/bin/
total 21872
-rwxr-xr-x 1 root root 2451088 Sep 22 16:19 redis-benchmark
-rwxr-xr-x 1 root root 5773320 Sep 22 16:19 redis-check-aof
-rwxr-xr-x 1 root root 5773320 Sep 22 16:19 redis-check-rdb
-rwxr-xr-x 1 root root 2617168 Sep 22 16:19 redis-cli
lrwxrwxrwx 1 root root 12 Sep 22 16:19 redis-sentinel -> redis-server
-rwxr-xr-x 1 root root 5773320 Sep 22 16:19 redis-server
[root@aliyun bin]#
この中にはredis内に建てられたshellがあります.
redis shell
起動と停止
redis-server
コマンドを使用して、プロファイルの読み取りとコマンドラインパラメータの指定をサポートする方法を起動します.一般的には、次の構成が含まれます.サービスの開始
redis-server
ヘルプ:[root@aliyun ~]# redis-server -h
Usage: ./redis-server [/path/to/redis.conf] [options] # option
./redis-server - (read config from stdin)
./redis-server -v or --version
./redis-server -h or --help
./redis-server --test-memory
Examples:
./redis-server (run the server with default conf)
./redis-server /etc/redis/6379.conf
./redis-server --port 7777
./redis-server --port 7777 --slaveof 127.0.0.1 8888 # 127.0.0.1:8888
./redis-server /etc/myredis.conf --loglevel verbose
Sentinel mode:
./redis-server /etc/sentinel.conf --sentinel
サービスの接続と使用
redis-cliという内蔵cliツールを使用するか、公式に提供されている様々なプログラミング言語のapiを使用してredisサービスに接続します.
[root@aliyun ~]# redis-cli
127.0.0.1:6379> set foo "hello redis"
OK
127.0.0.1:6379> get foo
"hello redis"
127.0.0.1:6379> exit
redis-cliには多くのパラメータがあり、インタラクティブ実行コマンドと非インタラクティブをサポートしています.
[root@aliyun ~]# redis-cli -h
redis-cli 4.0.10
Usage: redis-cli [OPTIONS] [cmd [arg [arg ...]]]
-h Server hostname (default: 127.0.0.1). server
-p Server port (default: 6379). , server
-s Server socket (overrides hostname and port).
-a Password to use when connecting to the server.
-u Server URI.
-r Execute specified command N times. N
-i When -r is used, waits seconds per command.
It is possible to specify sub-second times like -i 0.1.
-n Database number.
-x Read last argument from STDIN.
-d Multi-bulk delimiter in for raw formatting (default:
).
-c Enable cluster mode (follow -ASK and -MOVED redirections).
--raw Use raw formatting for replies (default when STDOUT is
not a tty).
--no-raw Force formatted output even when STDOUT is not a tty.
--csv Output in CSV format.
--stat Print rolling stats about server: mem, clients, ...
--latency Enter a special mode continuously sampling latency.
If you use this mode in an interactive session it runs
forever displaying real-time stats. Otherwise if --raw or
--csv is specified, or if you redirect the output to a non
TTY, it samples the latency for 1 second (you can use
-i to change the interval), then produces a single output
and exits.
--latency-history Like --latency but tracking latency changes over time.
Default time interval is 15 sec. Change it using -i.
--latency-dist Shows latency as a spectrum, requires xterm 256 colors.
Default time interval is 1 sec. Change it using -i.
--lru-test Simulate a cache workload with an 80-20 distribution.
--slave Simulate a slave showing commands received from the master.
--rdb Transfer an RDB dump from remote server to local file.
--pipe Transfer raw Redis protocol from stdin to server.
--pipe-timeout In --pipe mode, abort with error if after sending all data.
no reply is received within seconds.
Default timeout: 30. Use 0 to wait forever.
--bigkeys Sample Redis keys looking for big keys.
--hotkeys Sample Redis keys looking for hot keys.
only works when maxmemory-policy is *lfu.
--scan List all keys using the SCAN command.
--pattern Useful with --scan to specify a SCAN pattern.
--intrinsic-latency Run a test to measure intrinsic system latency.
The test will run for the specified amount of seconds.
--eval Send an EVAL command using the Lua script at .
--ldb Used with --eval enable the Redis Lua debugger.
--ldb-sync-mode Like --ldb but uses the synchronous Lua debugger, in
this mode the server is blocked and script changes are
are not rolled back from the server memory.
--help Output this help and exit.
--version Output version and exit.
Examples:
cat /etc/passwd | redis-cli -x set mypasswd
redis-cli get mypasswd
redis-cli -r 100 lpush mylist x
redis-cli -r 100 -i 1 info | grep used_memory_human:
redis-cli --eval myscript.lua key1 key2 , arg1 arg2 arg3
redis-cli --scan --pattern '*:12345*'
(Note: when using --eval the comma separates KEYS[] from ARGV[] items)
When no command is given, redis-cli starts in interactive mode.
Type "help" in interactive mode for information on available commands
and settings.
[root@aliyun ~]# redis-server &
または、プロファイルでdaemonizeをyesに設定し、起動時にプロファイルを指定します.
使用方法からredisは全体的にC-Sアーキテクチャのコンポーネントであり,mysqlなどのDBとは差が少ないように見え,サービス側のデータコンテナとしてクライアントがネットワーク(実際にはTCPプロトコルに基づくRDBP)を介してserverに接続し,データまたはメタデータを操作するコマンドを実行する.
サービスの停止
redisのK-Vデータ型
key
keyはstringタイプで、redisのhashtableのkeyです.
一般的な操作とコマンド
exists foo
set foo bar
get foo
type foo
expire foo seconds
expireat foo timestamp
pexpire foo milliseconds
persist key
pttl key
ttl key
randomkey
rename key newkey
renamenx key newkeynoexists
keys pattern
del foo
dbsize
127.0.0.1:6379> set m 100
OK
127.0.0.1:6379> get m
"100"
127.0.0.1:6379> ttl m
(integer) -1
127.0.0.1:6379> expire m 3
(integer) 1
127.0.0.1:6379> ttl m
(integer) 1
127.0.0.1:6379> ttl m
(integer) -2
127.0.0.1:6379> get m
(nil)
127.0.0.1:6379> set m hello
OK
127.0.0.1:6379> del m
(integer) 1
127.0.0.1:6379> get m
(nil)
127.0.0.1:6379>
value
valueには、次のようなデータ型があります.
type key
によって表示され得る.string
stringはredisの基礎データ型であり、keyのデータ型はstringであり、他の複合型のデータもstringに基づいている.stringは最大512 MBのデータを格納できる.
hash
すなわちHashMapはredisのインフラストラクチャと同様にkey-valueのデータ型である.hashのkeyとvalueはstringタイプ(hashのvalueがhashであるなどのネストされたデータ構造はサポートされていない)であり、hashのsizeは4バイト空間、すなわち32ビットのシンボル整形数の取値範囲である.
hashはjsonの簡略化されたフォーマットと同様にオブジェクトを記述するのに適している.データ型のネストはサポートされていないため、オブジェクトの属性がオブジェクトであるかというモデルを記述することはできない.同様のサポートのみ:
key field1 value1 field2 value2 ...
list 下位層は双方向チェーンテーブルで実現される線形データ構造であるため,ヘッダテールにノードを追加する効率は高いが,ランダムアクセス効率は劣る.容量空間も32ビットです.内部要素タイプはstringです.両端に要素を追加できます.
類似
key e1 e2 e3
set redisのSetはstring型の無秩序集合であり,要素は重複せず,重複追加は直接無視される.集合はハッシュテーブルによって実現されるので,追加,削除,検索の複雑さはいずれもO(1)である.集合は、交差、並列、差分などを求める操作を提供します.
sorted set
Setの要素に重みパラメータscoreを追加し、要素をscore順に並べます.hashテーブルとskiplistで実現されているので,中間位置のデータを読み取る速度も速い(O(lgN)).
Skip Listはランダム化されたデータ構造であり、並列チェーンテーブルに基づいており、その効率は二叉ルックアップツリー(ほとんどの操作に対してO(log n)平均時間を必要とする)よりも優れている.基本的には,ジャンプリストは秩序化されたチェーンテーブルに付加された前進リンクであり,増加はランダム化された方法で行われるため,リスト内の検索は部分リストを迅速にスキップすることができる(従って名付けられた).すべての操作は対数的にランダム化された時間で行われる.Skip Listは,秩序チェーンテーブルが特定の値を検索する困難をうまく解決できる.
参考資料