基于centos的redis与php

前端之家收集整理的这篇文章主要介绍了基于centos的redis与php前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

基于centos的redis与PHP

这是一个简要操作日记。。。

1、安装

安装PHPize。PHPize用于生成configure文件,为PHP扩展模块的编译生成配置文件

  1. yum install PHP-devel -y

安装redis

  1. yum install redis -y

从源码安装PHPredis

  1. wget https://github.com/nicolasff/PHPredis/archive/master.zip
  2. unzip master.zip
  3. cd PHPredis-master/
  4. PHPize
  5. ./configure
  6. make && make install && make test

配置PHP添加扩展

  1. find / -name PHP.ini
  2. vi /etc/PHP.ini
  3. G
  4. extension="redis.so"
  5. :wq
  6. service httpd restart # or reboot
  1. 测试PHPredis
  1. [root@nfvbfqi9 ~]# PHP -a
  2. Interactive shell
  3.  
  4.  
  5. PHP > $redis= new Redis();
  6. PHP > $redis->connect('127.0.0.1',6379);
  7. PHP > $redis->set("pageview",10);
  8. PHP > $k=$redis->get("pageview");
  9. PHP > echo $k;
  10. 10

2、简单操作

redis

  1. 启动redis服务

加$令其后台执行,加()表示在子shell中执行命令,纵然当前shell退出,此服务进程不会被kill。

  1. (redis-server&)
  1. 加入开机自启动脚本

注意>表示写文件会清空原有内容,>>表示追加到文件末尾行。

  1. echo "(redis-server&)" >>/etc/rc.local
  1. 启动redis命令行
  1. redis-cli #进入redis命令行模式
  1. 退出redis命令行
  1. quit

注意不要用shutdown。shutdown命令会关闭当前redis服务,也即kill掉redis服务进程

  1. dbsize #获取记录条数
  2. set pageview 0 #设置k:v
  3. get pageview #获取k:v
  4. del pageview #删除k:v
  5. save #保存
  6. help @generic #
  7.  
  8.  
  9. DEL key [key ...]
  10. summary: Delete a key
  11. since: 1.0.0
  12.  
  13. DUMP key
  14. summary: Return a serialized version of the value stored at the specified key.
  15. since: 2.6.0
  16.  
  17. EXISTS key [key ...]
  18. summary: Determine if a key exists
  19. since: 1.0.0
  20.  
  21. EXPIRE key seconds
  22. summary: Set a key's time to live in seconds
  23. since: 1.0.0
  24.  
  25. EXPIREAT key timestamp
  26. summary: Set the expiration for a key as a UNIX timestamp
  27. since: 1.2.0
  28.  
  29. KEYS pattern
  30. summary: Find all keys matching the given pattern
  31. since: 1.0.0
  32.  
  33. MIGRATE host port key| destination-db timeout [COPY] [REPLACE] [KEYS key]
  34. summary: Atomically transfer a key from a Redis instance to another one.
  35. since: 2.6.0
  36.  
  37. MOVE key db
  38. summary: Move a key to another database
  39. since: 1.0.0
  40.  
  41. OBJECT subcommand [arguments [arguments ...]]
  42. summary: Inspect the internals of Redis objects
  43. since: 2.2.3
  44.  
  45. PERSIST key
  46. summary: Remove the expiration from a key
  47. since: 2.2.0
  48.  
  49. PEXPIRE key milliseconds
  50. summary: Set a key's time to live in milliseconds
  51. since: 2.6.0
  52.  
  53. PEXPIREAT key milliseconds-timestamp
  54. summary: Set the expiration for a key as a UNIX timestamp specified in milliseconds
  55. since: 2.6.0
  56.  
  57. PTTL key
  58. summary: Get the time to live for a key in milliseconds
  59. since: 2.6.0
  60.  
  61. RANDOMKEY -
  62. summary: Return a random key from the keyspace
  63. since: 1.0.0
  64.  
  65. RENAME key newkey
  66. summary: Rename a key
  67. since: 1.0.0
  68.  
  69. RENAMENX key newkey
  70. summary: Rename a key,only if the new key does not exist
  71. since: 1.0.0
  72.  
  73. RESTORE key ttl serialized-value [REPLACE]
  74. summary: Create a key using the provided serialized value,prevIoUsly obtained using DUMP.
  75. since: 2.6.0
  76.  
  77. SCAN cursor [MATCH pattern] [COUNT count]
  78. summary: Incrementally iterate the keys space
  79. since: 2.8.0
  80.  
  81. SORT key [BY pattern] [LIMIT offset count] [GET pattern [GET pattern ...]] [ASC|DESC] [ALPHA] [STORE destination]
  82. summary: Sort the elements in a list,set or sorted set
  83. since: 1.0.0
  84.  
  85. TTL key
  86. summary: Get the time to live for a key
  87. since: 1.0.0
  88.  
  89. TYPE key
  90. summary: Determine the type stored at key
  91. since: 1.0.0
  92.  
  93. WAIT numslaves timeout
  94. summary: Wait for the synchronous replication of all the write commands sent in the context of the cu rrent connection
  95. since: 3.0.0
  96.  
  97. GEORADIUSBYMEMBER_RO key arg arg arg arg ...options...
  98. summary: Help not available
  99. since: not known
  100.  
  101. REPLCONF arg ...options...
  102. summary: Help not available
  103. since: not known
  104.  
  105. SUBSTR key arg arg arg
  106. summary: Help not available
  107. since: not known
  108.  
  109. PSYNC arg arg arg
  110. summary: Help not available
  111. since: not known
  112.  
  113. TOUCH key arg ...options...
  114. summary: Help not available
  115. since: not known
  116.  
  117. PFSELFTEST arg
  118. summary: Help not available
  119. since: not known
  120.  
  121. GEORADIUS_RO key arg arg arg arg arg ...options...
  122. summary: Help not available
  123. since: not known
  124.  
  125. POST arg ...options...
  126. summary: Help not available
  127. since: not known
  128.  
  129. LATENCY arg arg ...options...
  130. summary: Help not available
  131. since: not known
  132.  
  133. HOST: arg ...options...
  134. summary: Help not available
  135. since: not known
  136.  
  137. ASKING arg
  138. summary: Help not available
  139. since: not known
  140.  
  141. RESTORE-ASKING key arg arg arg ...options...
  142. summary: Help not available
  143. since: not known
  144.  
  145. PFDEBUG arg arg arg ...options...
  146. summary: Help not available
  147. since: not known

Then you know how to do IT.

猜你在找的CentOS相关文章