ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
# 这是什么 [http://www.redis.io/](http://www.redis.io/) # 安装 redis `apt-get install redis-server` `nohup redis-server &` 连接 `redis-cli` 自增 `incr num:key` 统计活跃度 月活跃度 ~~~ setbit string:count:huoyue:user:1 1 1 setbit string:count:huoyue:user:1 2 1 setbit string:count:huoyue:user:1 3 1 ~~~ 统计 `bitcount string:count:huoyue:user:1` setbit 实际上设置的是内存地址的字节 8位 00000000 setbit key 字节位置 1或者0 bitcount 统计内存地址中数据有多少个1 SESSION放到redis,比mencahe可靠,比mysql快。 # 哈希 设置哈希 ~~~ hset hash:user:1 username chensuilong hset hash:user:1 pwd 123456 hset hash:user:1 sex 1 ~~~ 读取哈希的值 `hget hash:user:1 username` 查询这个key下面的所有key.. `hgetall hash:user:1` # 列表 # 集合 # 有序集合 # 安装window版本的redis用于测试 [https://github.com/ServiceStack/redis-windows](https://github.com/ServiceStack/redis-windows) 解压以后,进入目录,运行 `redis-server.exe redis.windows.conf` 就能看到如下界面. ~~~ _._ _.-``__ ''-._ _.-`` `. `_. ''-._ Redis 3.0.501 (00000000/0) 64 bit .-`` .-```. ```\/ _.,_ ''-._ ( ' , .-` | `, ) Running in standalone mode |`-._`-...-` __...-.``-._|'` _.-'| Port: 6379 | `-._ `._ / _.-' | PID: 6584 `-._ `-._ `-./ _.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | http://redis.io `-._ `-._`-.__.-'_.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | `-._ `-._`-.__.-'_.-' _.-' `-._ `-.__.-' _.-' `-._ _.-' `-.__.-' [6584] 25 Apr 02:05:07.528 # Server started, Redis version 3.0.501 [6584] 25 Apr 02:05:07.528 * The server is now ready to accept connections on po rt 6379 ~~~ 证明redis服务已经开启. ## 客户端连接 直接输入`redis-cli.exe`即可. ~~~ F:\redis>redis-cli.exe 127.0.0.1:6379> set a name OK 127.0.0.1:6379> get a "name" 127.0.0.1:6379> del a (integer) 1 127.0.0.1:6379> get a (nil) 127.0.0.1:6379> ~~~ ## window下安装redis扩展 [http://windows.php.net/downloads/pecl/snaps/redis/20160319/](http://windows.php.net/downloads/pecl/snaps/redis/20160319/) 我这里用了php7,不知道为什么php5.6 ,4,3,都不行..那直接就上最新的7. 直接用php内置的服务器测试吧. `php -S localhost:8000` # php中使用redis ## 基本的玩法 其实只需要new出来,那么直接看redis的文档就行了 ~~~ <?php $redis = new redis(); $result = $redis->connect('127.0.0.1', 6379); var_dump($result); echo "set<br>"; $ret = $redis->set('name','hehe'); var_dump($ret); echo "<br><br>get<br>"; $ret = $redis->get('name'); var_dump($ret); echo "<br><br>delete<br>"; $ret = $redis->delete('name'); var_dump($ret); echo "<br><br>get<br>"; $ret = $redis->get('name'); var_dump($ret); echo "<br><br>setnx<br>"; $ret = $redis->set('name','1111111'); $ret = $redis->setnx('name','chensuilong'); var_dump($ret); echo "<br><br>get<br>"; $ret = $redis->get('name'); var_dump($ret); echo "<br><br>exists<br>"; $ret = $redis->exists('name'); var_dump($ret); echo "<br><br>exists<br>"; $ret = $redis->exists('name2'); var_dump($ret); echo "<br><br>incr<br>"; $ret = $redis->incr('num'); var_dump($ret); echo "<br><br>decr<br>"; $ret = $redis->decr('num.decr'); var_dump($ret); echo "<br><br>lpush<br>"; $ret = $redis->lpush('list','111'); var_dump($ret); echo "<br><br>rpush<br>"; $ret = $redis->rpush('list','222'); var_dump($ret); echo "<br><br>lpop<br>"; $ret = $redis->lpop('list'); var_dump($ret); echo "<br><br>lpop<br>"; $ret = $redis->lpop('list'); var_dump($ret); ?> ~~~ # 远程连接到服务器上的redis 设置配置文件 `/etc/redis/redis.conf` 把绑定ip修改下,不然其他IP连不上。 原来的 `bind 127.0.0.1` 修改为 `bind 0.0.0.0` > 未完待续