redis+mysql基本应用

WBOY
Release: 2016-06-06 20:16:04
Original
925 people have browsed it

初学redis,想了解一下redis+mysql的一些基本应用,一些解决方案,比如最常用的做缓存机制,一般怎么去实现呢?

回复内容:

初学redis,想了解一下redis+mysql的一些基本应用,一些解决方案,比如最常用的做缓存机制,一般怎么去实现呢?

redis做缓存放在php和mysql之间,就是利用了redis基于内存的高速数据读取特性,也是就是说,对于某些数据,当用户来访问你的web页面的时候,你先去从redis里面取出来,如果没有,再去从mysql取,展示给用户,同时写入redis,以便下次就能在redis里面取到了。
伪代码:

<code class="php">
$name = Redis::get('name');

if( !$name ){
    //缓存穿透,在Mysql取出并写入redis
    $name = Mysql::select('name');
    Redis::set('name',$name);
}
//...other code</code>
Copy after login

以上其实就是大多数仅仅拿redis来做缓存的大概原理。

当然,基于redis丰富的数据结构,还有很多其他用户,建议查阅相关教程或者文档。

redis我一般用来存session和缓存数据。session很好处理,不说了,说下数据缓存怎么做,最近看yii才学到的。

对于一张表的数据,假设有个last_mod_time字段,每次有数据更新的时候就必须同时更新这个时间,在查询表中数据的时候,先

<code>select avg(last_mod_time) from table where ...</code>
Copy after login

看看这个同样条件的集合,avg是不是变了,如果没变,直接从redis里面取得缓存数据,起到缓存作用。如果avg变了,那么将数据从mysql中查出来,并且根据where约束生成一个keyvalue就是数据集,这样的<key></key>保存进redis。大致就是这么个思路。

不要害怕,和普通架构是一样的。
就是在php与mysql层面中间加多一个cache缓存。

Redis自己的例子就给了不少应用。不同的API可以帮你做view,函数,手动等各个级别的cache。另外我用PostgreSQL,可以设置SQL查询自动cache,MySQL应该也可以。

将中文写入的redis中再读取的时候显示?

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!