1. 程式人生 > >redis在php中的應用(server篇)

redis在php中的應用(server篇)

mode del dir 快照 uil pri not 3.6 disk

Server(服務器)

1、BGREWRITEAOF

Redis Bgrewriteaof 命令用於異步執行一個 AOF(AppendOnly File) 文件重寫操作。重寫會創建一個當前 AOF 文件的體積優化版本。

即使 Bgrewriteaof 執行失敗,也不會有任何數據丟失,因為舊的 AOF 文件在 Bgrewriteaof 成功之前不會被修改。

註意:從 Redis 2.4 開始, AOF 重寫由 Redis 自行觸發, BGREWRITEAOF 僅僅用於手動觸發重寫操作。

語法:

redis 127.0.0.1:6379> BGREWRITEAOF

返回值: 反饋信息

可用版本:>= 1.0.0

時間復雜度:O(N), N 為要追加到 AOF 文件中的數據數量。

具體實例:

<?php
$redis = new redis();
$redis -> connect(‘127.0.0.1‘,6379);
$redis -> flushAll();

$redis -> set(‘job‘,‘programmer‘);
var_dump($redis -> bgrewriteaof());     // true ,會在 config[‘dir‘] 所定義的目錄下生產一個 dump.rdb 文件

2、BGSAVE

Redis Bgsave 命令用於在後臺異步保存當前數據庫的數據到磁盤

BGSAVE 命令執行之後立即返回 OK ,然後 Redis fork 出一個新子進程,原來的 Redis 進程(父進程)繼續處理客戶端請求,而子進程則負責將數據保存到磁盤,然後退出。

語法:

redis 127.0.0.1:6379> BGSAVE

返回值: 反饋信息

可用版本:>= 1.0.0

時間復雜度:O(N), N 為要保存到數據庫中的 key 的數量。

具體實例:

<?php
$redis = new redis();
$redis -> connect(‘127.0.0.1‘,6379);
$redis -> flushAll();

$redis -> set(‘job‘,‘programmer‘);
var_dump($redis -> bgsave());     // true , 子進程負責保存數據到磁盤

3、SAVE

Redis Save 命令執行一個同步保存操作,將當前 Redis 實例的所有數據快照(snapshot)以 RDB 文件的形式保存到硬盤。

語法:

redis 127.0.0.1:6379> SAVE

返回值: 保存成功時返回 OK 。

可用版本:>= 1.0.0

時間復雜度:O(N), N 為要保存到數據庫中的 key 的數量。

具體實例:

<?php
$redis = new redis();
$redis -> connect(‘127.0.0.1‘,6379);
$redis -> flushAll();

$redis -> set(‘job‘,‘programmer‘);
var_dump($redis -> save());     // true , 同步當前數據庫的數據到磁盤

4、LASTSAVE

Redis Lastsave 命令返回最近一次 Redis 成功將數據保存到磁盤上的時間,以 UNIX 時間戳格式表示。

語法:

redis 127.0.0.1:6379> LASTSAVE

返回值: 字符串,文本行的集合。

可用版本:>= 1.0.0

時間復雜度:O(1)

具體實例:

<?php
$redis = new redis();
$redis -> connect(‘127.0.0.1‘,6379);
$redis -> flushAll();

$redis -> set(‘job‘,‘programmer‘);
var_dump($redis -> lastSave());     // 1494838321

5、DBSIZE

dis Dbsize 命令用於返回當前數據庫的 key 的數量。

語法:

redis 127.0.0.1:6379> DBSIZE

返回值:當前數據庫的 key 的數量。

可用版本:>= 1.0.0

時間復雜度:O(1)

具體實例:

<?php
$redis = new redis();
$redis -> connect(‘127.0.0.1‘,6379);
$redis -> flushAll();

$redis -> set(‘job‘,‘programmer‘);
$redis -> set(‘favorite_fruit‘,‘cherry‘);
var_dump($redis -> dbSize());     // 2

6、SLAVEOF

Redis Slaveof 命令可以將當前服務器轉變為指定服務器的從屬服務器(slave server)。

(1)如果當前服務器已經是某個主服務器(master server)的從屬服務器,那麽執行 SLAVEOF host port 將使當前服務器停止對舊主服務器的同步,丟棄舊數據集,轉而開始對新主服務器進行同步

(2)對一個從屬服務器執行命令 SLAVEOF NO ONE 將使得這個從屬服務器關閉復制功能,並從從屬服務器轉變回主服務器,原來同步所得的數據集不會被丟棄。

(3)利用『 SLAVEOF NO ONE 不會丟棄同步所得數據集』這個特性,可以在主服務器失敗的時候,將從屬服務器用作新的主服務器,從而實現無間斷運行

語法:

redis 127.0.0.1:6379> SLAVEOF host port

返回值:總是返回 OK 。

可用版本:>= 1.0.0

時間復雜度:(1)SLAVEOF host port ,O(N), N 為要同步的數據數量。

      (2)SLAVEOF NO ONE , O(1) 。

具體實例:

<?php
$redis = new redis();
$redis -> connect(‘127.0.0.1‘,6379);
$redis -> flushAll();

$redis -> set(‘job‘,‘programmer‘);
$redis -> set(‘favorite_fruit‘,‘cherry‘);
var_dump($redis -> slaveof(‘127.0.0.1‘,‘6379‘));    // true  , 當前服務器從屬於 127.0.0.1 這臺服務器
var_dump($redis -> slaveof(‘NO ONE‘));              // true  ,當前服務器關閉復制功能,從從服務器變回主服務器,且數據不會丟失

7、FLUSHALL

Redis Flushall 命令用於清空整個 Redis 服務器的數據(刪除所有數據庫的所有 key )。

語法:

redis 127.0.0.1:6379> FLUSHALL

返回值:總是返回 OK 。

可用版本:>= 1.0.0

時間復雜度:尚未明確

具體實例:

<?php
$redis = new redis();
$redis -> connect(‘127.0.0.1‘,6379);
$redis -> flushAll();       // 清空整個 redis 服務器的數據

8、FLUSHDB

Redis Flushdb 命令用於清空當前數據庫中的所有 key。

語法:

redis 127.0.0.1:6379> FLUSHDB

返回值:總是返回 OK 。

可用版本:>= 1.0.0

時間復雜度:O(1)

具體實例:

<?php
$redis = new redis();
$redis -> connect(‘127.0.0.1‘,6379);
$redis -> flushDB();       // 清空當前數據庫中所有的 key

9、SLOWLOG

Redis Showlog 是 Redis 用來記錄查詢執行時間的日誌系統

查詢執行時間指的是不包括像客戶端響應(talking)、發送回復等 IO 操作,而單單是執行一個查詢命令所耗費的時間。

另外,slow log 保存在內存裏面,讀寫速度非常快,因此你可以放心地使用它,不必擔心因為開啟 slow log 而損害 Redis 的速度。

語法:

redis 127.0.0.1:6379> SLOWLOG subcommand [argument]

返回值:取決於不同命令,返回不同的值。

可用版本:>= 2.2.12

時間復雜度:O(1)

具體實例:

<?php
$redis = new redis();
$redis -> connect(‘127.0.0.1‘,6379);
//$redis -> slowlog(‘reset‘);                                       // 清空滿日子

var_dump($redis -> config(‘get‘,‘slowlog-log-slower-than‘));        // 1000 , 這是 redis 默認的時間,查詢時間大於 1s 的會被慢日誌記錄下來
var_dump($redis -> config(‘get‘,‘slowlog-max-len‘));                // 128 , 能保存128 條記錄,當記錄數量大於 128 時,最舊的一條記錄會被刪除,最新的一條記錄會被加入到 slowlog
var_dump($redis -> config(‘set‘,‘slowlog-log-slower-than‘,100));    // 修改時間
var_dump($redis -> config(‘get‘,‘*‘));      // 查詢所有的配置項
var_dump($redis -> slowlog(‘len‘));         // int 1 ,查看當前慢日誌的數量
var_dump($redis -> slowlog(‘get‘));         // 查看所有的慢日誌
var_dump($redis -> slowlog(‘get‘,1));       // 查看指定數量的慢日誌
//array (size=1)
//  0 =>
//    array (size=4)
//      0 => int 765            // slowlog 唯一編號ID,日誌的唯一 id 只有在 Redis 服務器重啟的時候才會重置,這樣可以避免對日誌的重復處理(比如你可能會想在每次發現新的慢查詢時發郵件通知你)。
//      1 => int 1494840849     // 查詢的時間戳
//      2 => int 1000           // 查詢耗時(微秒),如:本條命令查詢耗時 1000 微秒
//      3 =>
//        array (size=3)
//          0 => string ‘CONFIG‘ (length=6)     // 查詢命令,完整命令為 SLOWLOG GET,slowlog最多保存前面的31個key和128字符
//          1 => string ‘get‘ (length=3)
//          2 => string ‘*‘ (length=1)

10、INFO

Redis Info 命令以一種易於理解和閱讀的格式,返回關於 Redis 服務器的各種信息和統計數值

語法:

redis 127.0.0.1:6379> INFO [section]

返回值:字符串,文本行的集合。

可用版本:>= 1.0.0

時間復雜度:O(1)

具體實例:

<?php
$redis = new redis();
$redis -> connect(‘127.0.0.1‘,6379);

var_dump($redis -> info());
//array (size=83)
//  ‘redis_version‘ => string ‘3.0.503‘ (length=7)                 
//  ‘redis_git_sha1‘ => int 0                                     
//  ‘redis_git_dirty‘ => int 0                                      
//  ‘redis_build_id‘ => string ‘d14575c6134f877‘ (length=15)
//  ‘redis_mode‘ => string ‘standalone‘ (length=10)
//  ‘os‘ => string ‘Windows  ‘ (length=9)
//  ‘arch_bits‘ => int 64
//  ‘multiplexing_api‘ => string ‘WinSock_IOCP‘ (length=12)
//  ‘process_id‘ => int 13628
//  ‘run_id‘ => string ‘7ddea1b46590dfaa48665b4ec199bf8c4ecb71c3‘ (length=40)
//  ‘tcp_port‘ => int 6379
//  ‘uptime_in_seconds‘ => int 612896
//  ‘uptime_in_days‘ => int 7
//  ‘hz‘ => int 10
//  ‘lru_clock‘ => int 1669527
//  ‘config_file‘ => int 0
//  ‘connected_clients‘ => int 1
//  ‘client_longest_output_list‘ => int 0
//  ‘client_biggest_input_buf‘ => int 0
//  ‘blocked_clients‘ => int 0
//  ‘used_memory‘ => int 692408
//  ‘used_memory_human‘ => string ‘676.18K‘ (length=7)
//  ‘used_memory_rss‘ => int 633888
//  ‘used_memory_peak‘ => int 13492080
//  ‘used_memory_peak_human‘ => string ‘12.87M‘ (length=6)
//  ‘used_memory_lua‘ => int 36864
//  ‘mem_fragmentation_ratio‘ => string ‘0.92‘ (length=4)
//  ‘mem_allocator‘ => string ‘jemalloc-3.6.0‘ (length=14)
//  ‘loading‘ => int 0
//  ‘rdb_changes_since_last_save‘ => int 15
//  ‘rdb_bgsave_in_progress‘ => int 0
//  ‘rdb_last_save_time‘ => int 1494838761
//  ‘rdb_last_bgsave_status‘ => string ‘ok‘ (length=2)
//  ‘rdb_last_bgsave_time_sec‘ => int 0
//  ‘rdb_current_bgsave_time_sec‘ => string ‘-1‘ (length=2)
//  ‘aof_enabled‘ => int 0
//  ‘aof_rewrite_in_progress‘ => int 0
//  ‘aof_rewrite_scheduled‘ => int 0
//  ‘aof_last_rewrite_time_sec‘ => int 0
//  ‘aof_current_rewrite_time_sec‘ => string ‘-1‘ (length=2)
//  ‘aof_last_bgrewrite_status‘ => string ‘ok‘ (length=2)
//  ‘aof_last_write_status‘ => string ‘ok‘ (length=2)
//  ‘total_connections_received‘ => int 998
//  ‘total_commands_processed‘ => int 8119
//  ‘instantaneous_ops_per_sec‘ => int 0
//  ‘total_net_input_bytes‘ => int 361449
//  ‘total_net_output_bytes‘ => int 191765
//  ‘instantaneous_input_kbps‘ => string ‘0.00‘ (length=4)
//  ‘instantaneous_output_kbps‘ => string ‘0.00‘ (length=4)
//  ‘rejected_connections‘ => int 0
//  ‘sync_full‘ => int 0
//  ‘sync_partial_ok‘ => int 0
//  ‘sync_partial_err‘ => int 0
//  ‘expired_keys‘ => int 22
//  ‘evicted_keys‘ => int 0
//  ‘keyspace_hits‘ => int 4220
//  ‘keyspace_misses‘ => int 135
//  ‘pubsub_channels‘ => int 0
//  ‘pubsub_patterns‘ => int 0
//  ‘latest_fork_usec‘ => int 29003
//  ‘migrate_cached_sockets‘ => int 0
//  ‘role‘ => string ‘slave‘ (length=5)
//  ‘master_host‘ => string ‘NO ONE‘ (length=6)
//  ‘master_port‘ => int 6379
//  ‘master_link_status‘ => string ‘down‘ (length=4)
//  ‘master_last_io_seconds_ago‘ => string ‘-1‘ (length=2)
//  ‘master_sync_in_progress‘ => int 0
//  ‘slave_repl_offset‘ => int 1
//  ‘master_link_down_since_seconds‘ => string ‘jd‘ (length=2)
//  ‘slave_priority‘ => int 100
//  ‘slave_read_only‘ => int 1
//  ‘connected_slaves‘ => int 0
//  ‘master_repl_offset‘ => int 0
//  ‘repl_backlog_active‘ => int 0
//  ‘repl_backlog_size‘ => int 1048576
//  ‘repl_backlog_first_byte_offset‘ => int 0
//  ‘repl_backlog_histlen‘ => int 0
//  ‘used_cpu_sys‘ => string ‘3.56‘ (length=4)
//  ‘used_cpu_user‘ => string ‘0.87‘ (length=4)
//  ‘used_cpu_sys_children‘ => string ‘0.00‘ (length=4)
//  ‘used_cpu_user_children‘ => string ‘0.00‘ (length=4)
//  ‘cluster_enabled‘ => int 0
//  ‘db0‘ => string ‘keys=2,expires=0,avg_ttl=0‘ (length=26)

11、CONFIG GET

Redis Config Get 命令用於獲取 redis 服務的配置參數。

在 Redis 2.4 版本中, 有部分參數沒有辦法用 CONFIG GET 訪問,但是在最新的 Redis 2.6 版本中,所有配置參數都已經可以用 CONFIG GET 訪問了。

語法:

redis 127.0.0.1:6379> CONFIG GET parameter

返回值:給定配置參數的值。

可用版本:>= 2.0.0

具體實例:

<?php
$redis = new redis();
$redis -> connect(‘127.0.0.1‘,6379);

var_dump($redis -> config(‘get‘,‘requirepass‘));    // string ‘‘  , 獲取指定的配置項
var_dump($redis -> config(‘get‘,‘*‘));              // 獲取所有的配置項
//array (size=65)
//  ‘dbfilename‘ => string ‘dump.rdb‘ (length=8)
//  ‘requirepass‘ => string ‘‘ (length=0)
//  ‘masterauth‘ => string ‘‘ (length=0)
//  ‘unixsocket‘ => string ‘‘ (length=0)
//  ‘logfile‘ => string ‘‘ (length=0)
//  ‘pidfile‘ => string ‘/var/run/redis.pid‘ (length=18)
//  ‘maxmemory‘ => string ‘0‘ (length=1)
//  ‘maxmemory-samples‘ => string ‘5‘ (length=1)
//  ‘timeout‘ => string ‘0‘ (length=1)
//  ‘tcp-keepalive‘ => string ‘0‘ (length=1)
//  ‘auto-aof-rewrite-percentage‘ => string ‘100‘ (length=3)
//  ‘auto-aof-rewrite-min-size‘ => string ‘67108864‘ (length=8)
//  ‘hash-max-ziplist-entries‘ => string ‘512‘ (length=3)
//  ‘hash-max-ziplist-value‘ => string ‘64‘ (length=2)
//  ‘list-max-ziplist-entries‘ => string ‘512‘ (length=3)
//  ‘list-max-ziplist-value‘ => string ‘64‘ (length=2)
//  ‘set-max-intset-entries‘ => string ‘512‘ (length=3)
//  ‘zset-max-ziplist-entries‘ => string ‘128‘ (length=3)
//  ‘zset-max-ziplist-value‘ => string ‘64‘ (length=2)
//  ‘hll-sparse-max-bytes‘ => string ‘3000‘ (length=4)
//  ‘lua-time-limit‘ => string ‘5000‘ (length=4)
//  ‘slowlog-log-slower-than‘ => string ‘100‘ (length=3)
//  ‘latency-monitor-threshold‘ => string ‘0‘ (length=1)
//  ‘slowlog-max-len‘ => string ‘128‘ (length=3)
//  ‘port‘ => string ‘6379‘ (length=4)
//  ‘tcp-backlog‘ => string ‘511‘ (length=3)
//  ‘databases‘ => string ‘16‘ (length=2)
//  ‘repl-ping-slave-period‘ => string ‘10‘ (length=2)
//  ‘repl-timeout‘ => string ‘60‘ (length=2)
//  ‘repl-backlog-size‘ => string ‘1048576‘ (length=7)
//  ‘repl-backlog-ttl‘ => string ‘3600‘ (length=4)
//  ‘maxclients‘ => string ‘10000‘ (length=5)
//  ‘watchdog-period‘ => string ‘0‘ (length=1)
//  ‘slave-priority‘ => string ‘100‘ (length=3)
//  ‘min-slaves-to-write‘ => string ‘0‘ (length=1)
//  ‘min-slaves-max-lag‘ => string ‘10‘ (length=2)
//  ‘hz‘ => string ‘10‘ (length=2)
//  ‘cluster-node-timeout‘ => string ‘15000‘ (length=5)
//  ‘cluster-migration-barrier‘ => string ‘1‘ (length=1)
//  ‘cluster-slave-validity-factor‘ => string ‘10‘ (length=2)
//  ‘repl-diskless-sync-delay‘ => string ‘5‘ (length=1)
//  ‘cluster-require-full-coverage‘ => string ‘yes‘ (length=3)
//  ‘no-appendfsync-on-rewrite‘ => string ‘no‘ (length=2)
//  ‘slave-serve-stale-data‘ => string ‘yes‘ (length=3)
//  ‘slave-read-only‘ => string ‘yes‘ (length=3)
//  ‘stop-writes-on-bgsave-error‘ => string ‘yes‘ (length=3)
//  ‘daemonize‘ => string ‘no‘ (length=2)
//  ‘rdbcompression‘ => string ‘yes‘ (length=3)
//  ‘rdbchecksum‘ => string ‘yes‘ (length=3)
//  ‘activerehashing‘ => string ‘yes‘ (length=3)
//  ‘repl-disable-tcp-nodelay‘ => string ‘no‘ (length=2)
//  ‘repl-diskless-sync‘ => string ‘no‘ (length=2)
//  ‘aof-rewrite-incremental-fsync‘ => string ‘yes‘ (length=3)
//  ‘aof-load-truncated‘ => string ‘yes‘ (length=3)
//  ‘appendonly‘ => string ‘no‘ (length=2)
//  ‘dir‘ => string ‘D:\redis-3.0.503‘ (length=16)
//  ‘maxmemory-policy‘ => string ‘noeviction‘ (length=10)
//  ‘appendfsync‘ => string ‘everysec‘ (length=8)
//  ‘save‘ => string ‘jd 3600 jd 300 jd 60‘ (length=20)
//  ‘loglevel‘ => string ‘notice‘ (length=6)
//  ‘client-output-buffer-limit‘ => string ‘normal 0 0 0 slave 268435456 67108864 60 pubsub 33554432 8388608 60‘ (length=67)
//  ‘unixsocketperm‘ => string ‘0‘ (length=1)
//  ‘slaveof‘ => string ‘NO ONE 6379‘ (length=11)
//  ‘notify-keyspace-events‘ => string ‘‘ (length=0)
//  ‘bind‘ => string ‘‘ (length=0)

12、CONFIG SET

Redis Config Set 命令可以動態地調整 Redis 服務器的配置(configuration)而無須重啟。

你可以使用它修改配置參數,或者改變 Redis 的持久化(Persistence)方式。

語法:

redis 127.0.0.1:6379> CONFIG Set parameter value

返回值:當設置成功時返回 OK ,否則返回一個錯誤。

可用版本:>= 2.0.0

具體實例:

<?php
$redis = new redis();
$redis -> connect(‘127.0.0.1‘,6379);

var_dump($redis -> config(‘get‘,‘slowlog-log-slower-than‘));        // 1000
var_dump($redis -> config(‘set‘,‘slowlog-log-slower-than‘,100));    // true
var_dump($redis -> config(‘get‘,‘slowlog-log-slower-than‘));        // 100

redis在php中的應用(server篇)