美文网首页
2018-04-26-PHP 常见缓存

2018-04-26-PHP 常见缓存

作者: xiaojianxu | 来源:发表于2018-04-26 17:17 被阅读19次
cache_process.png

常见缓存分类

一、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 添加到配置文件中,动态加载扩展库配置项后面
phpinfo_memche_extension.png

参考手册

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;

相关文章

  • 2018-04-26-PHP 常见缓存

    常见缓存分类 如何选择缓存 使用频率,就是指使用次数的多少。 数据块大小,就是指数据大小。 作用域,就是指作用的范...

  • Redis缓存雪崩、缓存击穿、缓存穿透、缓存预热、缓存更新、缓存

    关于Redis常见问题:缓存雪崩、缓存穿透、缓存预热、缓存更新、缓存降级等概念的入门及简单解决方案。 一、缓存雪崩...

  • Guava Cache 使用

    缓存分为本地缓存和远端缓存。常见的远端缓存有 Redis,MongoDB;本地缓存一般使用 map 的方式保存在本...

  • Guava cache使用总结

    缓存分为本地缓存和远端缓存。常见的远端缓存有Redis,MongoDB;本地缓存一般使用map的方式保存在本地内存...

  • 大型分布式系统中的缓存架构

    本文主要介绍大型分布式系统中缓存的相关理论,常见的缓存组件以及应用场景。 缓存概述 缓存概述 缓存的分类 缓存主要...

  • 大型分布式系统中的缓存架构

    本文主要介绍大型分布式系统中缓存的相关理论,常见的缓存组件以及应用场景。 缓存概述 缓存概述 缓存的分类 缓存主要...

  • Redis原理&设计

    缓存原理&设计 本章学习目标 理解缓存的使用场景(重点) 理解缓存原理(重点) 了解常见缓存以及分类(重点) 理解...

  • 互联网常见的四大技术

    互联网常见的四大技术 缓存 限流 熔断降级 隔离或分离 1.缓存 缓存类型:分布式缓存(代表redis)、本地缓存...

  • 深入理解分布式系统中的缓存架构(上)

    本文主要介绍大型分布式系统中缓存的相关理论,常见的缓存组件以及应用场景。 1 缓存概述 缓存概述 2 缓存的分类 ...

  • Android缓存浅析

    Android缓存浅析 By吴思博 1、引言 2、常见的几种缓存算法 3、Android缓存的机制 4、LruCa...

网友评论

      本文标题:2018-04-26-PHP 常见缓存

      本文链接:https://www.haomeiwen.com/subject/bajmlftx.html