常见缓存分类
一、PHP 编译缓存,有 xcache、eaccelerator、apc
二、PHP 内存缓存,有 Memcache、Redis
三、文件缓存,就是普通磁盘缓存
如何选择缓存
从以下三个方面,使用频率、数据块大小、作用域
使用频率,就是指使用次数的多少。
数据块大小,就是指数据大小。
作用域,就是指作用的范围。如:模板缓存,数据库查询缓存,编译代码缓存。。。。
通常各种类型缓存是交叉使用的,一个系统里,既有内存缓存,又有文件缓存,甚至还会有编译缓存。
- redis 是什么?
Redis is an open source (BSD licensed), [in-memory data structure store, used as a database, cache and message broker]. It supports data structures such as strings, hashes, lists, sets, sorted sets with range queries, bitmaps, hyperloglogs and geospatial indexes with radius queries. Redis has built-in replication, Lua scripting, LRU eviction, transactions and different levels of on-disk persistence, and provides high availability via Redis Sentinel and automatic partitioning with Redis Cluster.
- memcache 是什么?
What is Memcached?
Free & open source, high-performance, distributed memory object caching system, generic in nature, but intended for use in speeding up dynamic web applications by alleviating database load.
Memcached is an in-memory key-value store for small chunks of arbitrary data (strings, objects) from results of database calls, API calls, or page rendering.
Memcached is simple yet powerful. Its simple design promotes quick deployment, ease of development, and solves many problems facing large data caches. Its API is available for most popular languages.
redis([in-memory data structure store, used as a database, cache and message broker]) 和 memecache(distributed memory object caching system) 的不同
1、存储方式
memecache 把数据全部存在内存中,断电后会挂掉,数据不能超过内存大小。
redis 可以通过配置,保证数据的持久性。
(12306, 淘宝双 11)
2、支持数据类型
redis 支持的数据类型: strings(字符), hashes(哈希表), lists(列表), sets(集合), sorted sets with range queries(有序集合), bitmaps, hyperloglogs and
geospatial indexes radius queries
memcache 支持的数据类型有: strings, objects, array
memcache expire time(失效时间)
Expiration times are specified in unsigned integer seconds. They can be set from 0, meaning "never expire", to 30 days (60*60*24*30). Any time higher than 30 days is interpreted as a unix timestamp date. If you want to expire an object on january 1st of next year, this is how you do that.
For binary protocol an expiration must be unsigned. If a negative expiration is given to the ASCII protocol, it is treated it as "expire immediately".
Key Usage
Thinking about your keys can save you a lot of time and memory. Memcached is a hash, but it also remembers the full key internally.
The longer your keys are, the more bytes memcached has to hash to look up your value, and the more memory it wastes storing a full copy of your key.
On the other hand, it should be easy to figure out exactly where in your code a key came from. Otherwise many laborous hours of debugging wait for you.
在 Windows 安装使用 memcache
一、安装 memcache
Windows 32 bit 版本 — static.runoob.com/download/memcached-win32-1.4.4-14.zip
Windows 64 bit 版本 — static.runoob.com/download/memcached-win64-1.4.4-14.zip
二、将压缩包解压,进行安装
安装 - 解压的目录/memcached.exe -d install
运行 - 解压的目录/memcached.exe -d start
停止 - 解压的目录/memcached.exe -d stop
三、安装 memcache 的 PHP 扩展
https://windows.php.net/downloads/pecl/releases/memcache/3.0.8/
解压文件,将 php_memcache.dll 文件,拷贝到 『你的PHP目录/ext』 中;
四、修改你服务器环境所使用的 PHP 配置文件: php.ini,将 extension=php_memcache.dll 添加到配置文件中,动态加载扩展库配置项后面
参考手册
redis 手册: http://redisdoc.com/topic/persistence.html#redis
memcache 手册: http://memcached.org/
memcache - github 手册: https://github.com/memcached/memcached/wiki
课后练习
1、 在阿里云安装 memcache,并实现 mysql 数据缓存案例;
2、在阿里云安装 redis,并实现 php 使用 redis;