The following class encapsulates memcache, including adding, reading, clearing, deleting, obtaining server information, caching service pool, etc.
<code><span><span><span><?php </span>/******************************************* * 文件名: /includes/memcache.class.php * 功能: memcache 缓存类 * 版本: 1.0 * 日期: 2016-07-16 * 程序名: memcache缓存操作类 -----(PHP中需加载memcache扩展) * 作者: JoeXiong * 版权: Copyright@2016-2016 github.com/JoeXiong All Rights Reserved *********************************************/</span><span><span>class</span><span>joememcache</span> {</span><span>private</span><span>static</span><span>$_instance</span>; <span>private</span><span>$_memcache</span>; <span>private</span><span>$_which</span> = <span>0</span>; <span>private</span><span>$_memservers</span>; <span>public</span><span>static</span><span><span>function</span><span>getInstance</span><span>()</span> {</span><span>if</span> (! (<span>self</span>::<span>$_instance</span><span>instanceof</span> joememcache)) { <span>self</span>::<span>$_instance</span> = <span>new</span><span>self</span>(); } <span>return</span> is_object(<span>self</span>::<span>$_instance</span>->_memcache) ? <span>self</span>::<span>$_instance</span> : <span>false</span>; } <span>private</span><span><span>function</span><span>__construct</span><span>()</span> {</span><span>if</span> (extension_loaded(<span>'memcache'</span>)) { <span>$this</span>->_memcache = <span>new</span> Memcache(); <span>$this</span>->_which = <span>1</span>; } <span>elseif</span> (extension_loaded(<span>'memcached'</span>)) { <span>$this</span>->_memcache = <span>new</span> Memcached(); <span>$this</span>->_which = <span>2</span>; } <span>else</span><span>$this</span>->_memcache = <span>FALSE</span>; } <span>/** * 保存缓存 *<span> @param</span> unknown $key *<span> @param</span> unknown $data *<span> @param</span> number $ttl *<span> @param</span> string $isCompress *<span> @return</span> boolean */</span><span>public</span><span><span>function</span><span>Save</span><span>(<span>$key</span>, <span>$data</span>, <span>$ttl</span> = <span>60</span>,<span>$isCompress</span> = FALSE)</span>{</span><span>if</span>(<span>$this</span>->_which == <span>1</span>) <span>return</span><span>$this</span>->_memcache->set(<span>$key</span>, <span>array</span>(<span>$data</span>, time(), <span>$ttl</span>), !<span>$isCompress</span> ? <span>0</span> : MEMCACHE_COMPRESSED, <span>$ttl</span>);<span>//使用time() 函数最新</span><span>else</span><span>if</span>(<span>$this</span>->_which == <span>2</span>) <span>return</span><span>$this</span>->_memcache->set(<span>$key</span>, <span>array</span>(<span>$data</span>, time(), <span>$ttl</span>), <span>$ttl</span>); <span>else</span><span>return</span><span>FALSE</span>; } <span>/** * 读取缓存信息 *<span> @param</span> unknown $key *<span> @return</span> Ambigous <multitype: string>|boolean */</multitype:></span><span>public</span><span><span>function</span><span>readMetaData</span><span>(<span>$key</span>)</span>{</span><span>$value</span> = <span>$this</span>->_memcache->get(<span>$key</span>); <span>if</span>(is_array(<span>$value</span>) && count(<span>$value</span>) == <span>3</span>){ <span>list</span>(<span>$data</span>, <span>$time</span>, <span>$ttl</span>) = <span>$value</span>; <span>return</span> (time() $time</span> + <span>$ttl</span>) ? <span>$data</span> : <span>array</span>(); } <span>else</span> { <span>return</span><span>false</span>; } } <span>/** *<span> @deprecated</span> 读取多个缓存信息 *<span> @param</span> array $array *<span> @return</span> 成功$value(array), 失败FALSE(bool) */</span><span>public</span><span><span>function</span><span>readMultiData</span><span>(<span>$keys</span>)</span> {</span><span>if</span>(!is_array(<span>$keys</span>)) { <span>return</span><span>FALSE</span>; } <span>$rtn</span> = <span>array</span>(); <span>if</span>(<span>$this</span>->_which == <span>1</span>) <span>$rtn</span> = <span>$this</span>->_memcache->get(<span>$keys</span>); <span>else</span><span>if</span>(<span>$this</span>->_which == <span>2</span>) <span>$rtn</span> = <span>$this</span>->_memcache->getMulti(<span>$keys</span>); <span>$now</span> = time(); <span>foreach</span>(<span>$rtn</span><span>as</span><span>$key</span>=>&<span>$v</span>) { <span>if</span>(!<span>empty</span>(<span>$v</span>)) { <span>list</span>(<span>$data</span>, <span>$time</span>, <span>$ttl</span>) = <span>$v</span>; <span>$v</span> = (<span>$now</span> $time</span> + <span>$ttl</span>) ? <span>$data</span> : <span>array</span>(); } } <span>return</span><span>$rtn</span>; } <span>/** *<span> @description</span> 读取缓存 *<span> @param</span> $key 查询索引key *<span> @return</span> 成功 array 失败 FALSE */</span><span>public</span><span><span>function</span><span>Read</span><span>(<span>$key</span>)</span> {</span><span>$data</span> = <span>$this</span>->_memcache->get(<span>$key</span>); <span>return</span> is_array(<span>$data</span>) ? <span>$data</span>[<span>0</span>] : <span>FALSE</span>; } <span>/** *<span> @description</span> 删除缓存 *<span> @param</span> $key 将要删除的key * return bool 成功 TRUE 失败 FALSE */</span><span>public</span><span><span>function</span><span>Delete</span><span>(<span>$key</span>)</span> {</span><span>return</span><span>$this</span>->_memcache->delete(<span>$key</span>); } <span>/** *<span> @description</span> 清空所有缓存 *<span> @return</span> bool true or false */</span><span>public</span><span><span>function</span><span>Clear</span><span>()</span> {</span><span>return</span><span>$this</span>->_memcache->flush(); } <span>/** * 获取缓存服务器池中所有服务器统计信息 *<span> @return</span> array */</span><span>public</span><span><span>function</span><span>getExtendedStats</span><span>()</span> {</span><span>//return $this->_memcache->getExtendedStats();</span><span>if</span>(<span>$this</span>->_which == <span>1</span>) { <span>return</span><span>$this</span>->_memcache->getExtendedStats(); } <span>else</span><span>if</span>(<span>$this</span>->_which == <span>2</span>) <span>return</span><span>$this</span>->_memcache->getServerList(); <span>else</span><span>return</span><span>FALSE</span>; } <span>/** * 缓存服务器池 */</span><span>public</span><span><span>function</span><span>addServer</span><span>()</span> {</span><span>foreach</span> (<span>$this</span>->_memservers <span>as</span><span>$h</span>) { <span>$this</span>->_memcache->addServer(<span>$h</span>[<span>'host'</span>], <span>isset</span>(<span>$h</span>[<span>'port'</span>]) ? <span>$h</span>[<span>'port'</span>] : <span>11211</span>); <span>// 默认端口为11211</span> } } <span>/** * 获取memecache服务器地址 */</span><span>public</span><span><span>function</span><span>getHost</span><span>()</span> {</span><span>return</span><span>$this</span>->_memservers; } <span>/** * 设置memcache服务器地址 */</span><span>public</span><span><span>function</span><span>setHost</span><span>(array <span>$servers</span>)</span> {</span><span>if</span> (is_array(<span>$servers</span>) && ! <span>empty</span>(<span>$servers</span>)) { <span>$this</span>->_memservers = <span>$servers</span>; <span>$this</span>->addServer(); } } <span>private</span><span><span>function</span><span>__clone</span><span>()</span> {</span>} }</code>
The following is how to use the encapsulated class
<code><span>//初始化memcache</span><span>$memcache</span> = joememcache::getInstance(); <span>//是否开启memcache扩展</span><span>if</span>(<span>$memcache</span>){ <span>//memcache的配置参数,放在配置文件中</span><span>global</span><span>$memServer</span>,<span>$memKey</span>; <span>//设置memecache服务器地址</span><span>$memcache</span>->setHost(<span>$memServer</span>); <span>//通过key读取数据</span><span>$news</span> = <span>$memcache</span>->readMetaData(<span>$memKey</span>[<span>'sharetypekey'</span>][<span>'10001'</span>][<span>'Key'</span>].<span>$id</span>); <span>//memcache没有当前key的数据,则从数据库查询并保存到memecache服务器中</span><span>if</span>(!<span>$news</span>){ <span>$news</span> = <span>$this</span>->getCurSharenewsInfo(<span>$id</span>); <span>$memcache</span>->Save(<span>$memKey</span>[<span>'sharetypekey'</span>][<span>'10001'</span>][<span>'Key'</span>].<span>$id</span>,<span>$news</span>,<span>$memKey</span>[<span>'sharetypekey'</span>][<span>'10001'</span>][<span>'Time'</span>]); } }<span>else</span>{ <span>//直接数据库查询数据</span><span>$news</span> = <span>$this</span>->getCurSharenewsInfo(<span>$id</span>); }</code>').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });
The above has introduced the detailed introduction of the PHP-Mmecache operation class, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

在Web应用中,缓存是一个非常重要的技术。缓存可以大大减少数据库和服务器的负载,提高Web应用的性能。Memcache是一种高性能的分布式内存缓存系统,常用于Web应用中。在PHP中使用Memcache缓存,有时会出现一些问题,本文将介绍这些问题及其解决办法。问题一:无法连接到Memcache服务器在使用Memcache缓存时,第一个遇到的问题可能是无法连接

随着互联网的飞速发展,越来越多的应用程序需要面对大量的并发请求,如何提高应用的并发处理能力成为开发者们需要解决的问题。其中,利用Memcache缓存技术进行并发优化成为了相对较为流行的一种方案。Memcache是一种高效的缓存技术,适用于大型Web应用程序、数据库和分布式系统。其特点是将数据存储于内存中,以实现高速读写操作。在Web应用程序的数据访问过程中,

Memcache是一种在Web应用中常用的缓存技术,对于高并发的应用,它能够减轻数据库的压力,提高数据读取速度,降低系统响应时间。但是,在实际运用中,由于某些原因,会出现缓存数据被破坏的情况。本文主要从以下几个方面来讲述如何避免PHP应用中Memcache缓存技术出现数据损坏的情况。一、数据序列化通常情况下,我们将需要缓存的数据直接以对象形式存储到Memca

Memcache是一种开源的、分布式的缓存技术。它通过将数据存储在内存中,极大地提高了数据的访问速度,从而提升了网站的性能和响应速度。在PHP项目中,Memcache缓存技术也被广泛应用,并且取得了很好的效果。本篇文章将深入探讨Memcache缓存技术在PHP项目中的应用和实践。一、Memcache的原理和优势Memcache是一种内存缓存技术,它可以将数据

随着网络技术越来越发达,网站的访问量逐渐增多,为了提升用户体验,我们需要尽可能地减少网页的加载时间和传输数据的大小。其中,Gzip压缩算法是一种经典的数据压缩算法,可以在传输数据时将数据压缩,减少传输数据的大小,从而提升网页的加载速度和用户体验。在使用Gzip压缩算法来优化网站时,我们还可以结合PHP中的Memcache缓存技术来进一步提升网站的性能。一、G

Memcache缓存技术在PHP中优化数据交互的实践和思考在现代的Web应用中,数据交互是一个非常重要的问题,它没有足够的高效性,将会限制Web应用程序的扩展性和性能。为了加快数据交互速度,我们通常的做法是优化数据库的设计、提高硬件的性能和增加服务器容量。但是,这些方法都有一个共同的限制:它们会增加系统的成本。最近几年,Memcache技术在解决这个问题上提

随着互联网技术的不断发展,网站的用户访问量越来越大,带来的并发访问量也越来越高。为了应对这种高并发访问,常用的手段是使用缓存技术。而在PHP语言中,Memcache缓存技术是一种非常有效的解决方案。Memcache是一种分布式缓存系统,能够将大量的数据缓存在内存中,并能够从内存中快速读取,从而提高网站的响应速度和并发能力。在本文中,我们将介绍如何使用PHP中

随着互联网技术的发展,网络爬虫越来越被用于数据挖掘、搜索引擎等领域。而大规模的数据采集和处理不仅需要更高效的爬虫算法,同时需要优化处理数据的速度和减少资源消耗。在这个过程中,缓存技术发挥了重要作用,为数据处理和应用的性能提供了帮助。本文介绍如何使用PHP中的Memcache缓存技术对于爬虫进行优化。Memcache是一个高性能的分布式内存对象缓存系统。Mem


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

WebStorm Mac version
Useful JavaScript development tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.
