redis 在 php 中的应用,Server[ 服务器] 篇

本文为我阅读了 redis参考手册 之后编写,注意 php_redis 和 redis-cli 的区别(主要是返回值类型和参数用法)

目录:

Server(服务器)
BGREWRITEAOFBGSAVESAVELASTSAVEDBSIZESLAVEOF
FLUSHALLFLUSHDBSLOWLOGINFOCONFIG GETCONFIG SET

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

如有转载,请注明出处:http://www.cnblogs.com/chrdai/p/6857518.html