本篇为基础篇
Redis的使用场景想必大家多多少少都了解一些了。比如新浪的首页那么多模块,那么多文章,如果读数据库是不是压力特别大,反应是不是特别慢?但是为什么新浪为什么能很快的响应页面?其中一部分功劳就是靠的Reids的缓存技术。相比较Memcached笔者还是更喜欢Redis一点。
下面简单的分析一下,欢迎拍砖!
-
Redis不仅仅支持简单的k/v类型的数据,同时还提供list,set,hash等数据结构的存储。
-
Redis支持数据的备份,即master-slave模式的数据备份。
-
Redis支持数据的持久化,可以将内存中的数据保持在磁盘中,重启的时候可以再次加载进行使用。
Laravel中 使用的Redis
Redis 是一款开源且先进的键值对数据库。由于它可用的键包含了字符串、哈希、列表、集合 和 有序集合,因此常被称作数据结构服务器。在使用 Redis 之前,你必须通过 Composer 安装 predis/predis 扩展包(~1.0)。
安装predis组件
composer require "predis/predis:~1.0"
配置 应用程序的 Redis 设置都在 config/database.php 配置文件中。在这个文件里,你可以看到 redis 数组里面包含了应用程序使用的 Redis 服务器:
- 'redis' => [
-
- 'cluster' => false,
-
- 'default' => [
- 'host' => '127.0.0.1',
- 'port' => 6379,
- 'database' => 0,
- ],
-
- ],
默认的服务器配置对于开发来说应该足够了。然而,你也可以根据使用的环境来随意更改数组。只需给每个 Redis 指定名称以及在服务器中使用的 host 和 port 即可。
基本使用方法
STRING类型 - 写入字符串类型的redis
- class PhotoController extends Controller
- {
- /**
- * Display a listing of the resource.
- *
- * @return \Illuminate\Http\Response
- */
- public function index()
- {
- //
- $key = 'STRING:TEST';
- $value = 'Hello-World';
- // 写入一个字符串类型的redis
- $info = \Redis::Set($key,$value);
- dd($info);
- return view('test');
- }
- }
-
页面响应 
-
读取相应的字符串
- class PhotoController extends Controller
- {
- /**
- * Display a listing of the resource.
- *
- * @return \Illuminate\Http\Response
- */
- public function index()
- {
- //
- $key = 'STRING:TEST';
- // 读取一个字符串类型的redis
- $info = \Redis::get($key);
- dd($info);
- return view('test');
- }
- }
- 页面响应 
和redis语法同样的 字串也有incr和decr等递增、递减...
LIST类型
- 写入队列
- class PhotoController extends Controller
- {
- /**
- * Display a listing of the resource.
- *
- * @return \Illuminate\Http\Response
- */
- public function index()
- {
- //
- $key = 'LIST:TEST:R';
- $names = ['PHP','HTML','CSS','JavaScript','Node','Java','Ruby','Python'];
- // 从右往左压入队列
- $info = \Redis::rpush($key,$names);
- dd($info);
- return view('test');
- }
- }
-
页面响应 (写入的数量) 
-
写入队列
- class PhotoController extends Controller
- {
- /**
- * Display a listing of the resource.
- *
- * @return \Illuminate\Http\Response
- */
- public function index()
- {
- //
- $key = 'LIST:TEST:R';
- $names = ['PHP','HTML','CSS','JavaScript','Node','Java','Ruby','Python'];
- // 获取队列内容(0到-1 所有 0到0是一位 0到1是两位)
- $info = \Redis::lrange($key,0,-1);
- dd($info);
- return view('test');
- }
- }
-
页面响应 (数组) 
-
从左往右塞入队列 连贯方法
- class PhotoController extends Controller
- {
- /**
- * Display a listing of the resource.
- *
- * @return \Illuminate\Http\Response
- */
- public function index()
- {
- //
- $key = 'LIST:TEST:L';
- $names = ['PHP','HTML','CSS','JavaScript','Node','Java','Ruby','Python'];
- // 从左往右存数据
- \Redis::lpush($key,$names);
- // 取出数据
- $info = \Redis::lrange($key,0,-1);
- dd($info);
- return view('test');
- }
- }
- 页面响应 (数组 是不是正好和上面的相反?) 
HASH类型
- 存数据
- class PhotoController extends Controller
- {
- /**
- * Display a listing of the resource.
- *
- * @return \Illuminate\Http\Response
- */
- public function index()
- {
- //
- $key = 'HASH:TEST';
- $names = ['id'=>'99',
- 'name'=>'AXiBa',
- 'age'=>'23',
- 'tel'=>'13995578699',
- 'addtime'=>'1231231233'];
- // 将数据写入hash
- $info = \Redis::hMset($key,$names);
- dd($info);
- return view('test');
- }
- }
-
页面响应 
-
取数据(取所有)
- class PhotoController extends Controller
- {
- /**
- * Display a listing of the resource.
- *
- * @return \Illuminate\Http\Response
- */
- public function index()
- {
- //
- $key = 'HASH:TEST';
- $names = ['id'=>'99',
- 'name'=>'AXiBa',
- 'age'=>'23',
- 'tel'=>'13995578699',
- 'addtime'=>'1231231233'];
- // 取出hash里的数据
- $info = \Redis::hGetall($key);
- dd($info);
- return view('test');
- }
- }
-
页面响应 
-
取数据(取个别字段)
- class PhotoController extends Controller
- {
- /**
- * Display a listing of the resource.
- *
- * @return \Illuminate\Http\Response
- */
- public function index()
- {
- //
- $key = 'HASH:TEST';
- $names = ['id'=>'99',
- 'name'=>'AXiBa',
- 'age'=>'23',
- 'tel'=>'13995578699',
- 'addtime'=>'1231231233'];
- // 取出hash里的 某一个字段的数据
- $info = \Redis::hGet($key,'name');
- dd($info);
- return view('test');
- }
- }
- 页面响应 
- // 判断这个redis key是否存在
- \Redis::exists($key);
SET类型
- 写入一个无序集合(数据插入无顺序)
- class PhotoController extends Controller
- {
- /**
- * Display a listing of the resource.
- *
- * @return \Illuminate\Http\Response
- */
- public function index()
- {
- //
- $key = 'SET:TEST';
- $value = ['a','b','c','d','e'];
- $info = \Redis::sadd($key,$value);
- $info = \Redis::smembers($key);
- dd($info);
- return view('test');
- }
- }
-
页面响应 
-
求两个集合的交集
- class PhotoController extends Controller
- {
- /**
- * Display a listing of the resource.
- *
- * @return \Illuminate\Http\Response
- */
- public function index()
- {
- //
- $key = 'SET:TEST';
- $key1 = 'SET:TEST:1';
- $value = ['a','b','c','d','e'];
- $value1 = ['a','b','c','1','2'];
- // 写入另一个集合
- \Redis::sadd($key1,$value1);
- // 交集
- $info = \Redis::sinter($key,$key1);
- dd($info);
- return view('test');
- }
- }
-
页面响应 
-
求两个集合的并集
- class PhotoController extends Controller
- {
- /**
- * Display a listing of the resource.
- *
- * @return \Illuminate\Http\Response
- */
- public function index()
- {
- //
- $key = 'SET:TEST';
- $key1 = 'SET:TEST:1';
- $value = ['a','b','c','d','e'];
- $value1 = ['a','b','c','1','2'];
- // 并集
- $info = \Redis::sunion($key,$key1);
- dd($info);
- return view('test');
- }
- }
-
页面响应 
-
求两个集合的差集
- class PhotoController extends Controller
- {
- /**
- * Display a listing of the resource.
- *
- * @return \Illuminate\Http\Response
- */
- public function index()
- {
- //
- $key = 'SET:TEST';
- $key1 = 'SET:TEST:1';
- $value = ['a','b','c','d','e'];
- $value1 = ['a','b','c','1','2'];
- // 差集
- $info = \Redis::sdiff($key,$key1);
- dd($info);
- return view('test');
- }
- }
哪个key在前,就以哪个key的值为基准。。
- 页面响应 
当然了,这里只是一些最基本的Redis缓存demo,其实他的强大远远不止这些,操作也不止这些,比如队列里的弹出,比如集合与集合之间的复杂关系运用...如何将非关系型的Redis运用成关系型数据库那样??Redis的一些实用场景又是那一些??敬请查看下一篇--"Redis 三部曲之第二部 Redis 基本的数据隔离"。
[原文地址](http://www.blog8090.com/)