在项目中,涉及大访问量时,合理的使用缓存能减轻数据库的压力,同时提升用户体验。即在非实时性的需求的前提下,一小段时间内(若干秒),用于显示的数据从缓存中获取的,而不用直接读取数据库,能有效的减少数据库的读取压力。这里记录一下php语言使用memcache的情形:
首先,我们建立一个memcachepool,可以根据不同的配置读取,生成不同的memcache实例。用到$memcache->addServer($host,$port,$flag);向连接池中添加一个memcache服务器。代码示例如下:
class memcachePool{ private static $instance; private $memcacheList = array(); private function __construct(){ } public static function getInstance(){ if(self::$instance != null) return self::$instance; self::$instance = new memcachePool(); return self::$instance; } /** * get memcache object from pool * @param [type] $host 服务器 * @param [type] $port 端口 * @param [type] $flag 控制是否使用持久化连接。默认TRUE * @return [type] */ public function getMemcache($host,$port,$flag){ if(isset($this->memcacheList[$host.$port])) return $this->memcacheList[$host.$port]; $memcache = new Memcache(); // 向连接池中添加一个memcache服务器 $memcache->addServer($host,$port,$flag); //开启大值自动压缩,第一个参数表示处理数据大小的临界点,第二个参数表示压缩的比例,默认为0.2 $memcache->setCompressThreshold(2000,0.2); $this->memcacheList[$host.$port] = $memcache; return $memcache; } }
接着实现一个包含memcache常用方法如add,set,get,flush,delete等的方法类,这里命名为dlufmemcache
class dlufMemcache{ private $memcache = null; function __construct($host,$port){ $this->memcache = memcachepool::getInstance()->getMemcache($host,$port,true); } /** * memcache set value * @param [type] $key 键 * @param [type] $value 值 * @param integer $expire 到期的时间,如果此值设置为0表明此数据永不过期 * @param integer $flag 标志位 使用MEMCACHE_COMPRESSED指定对值进行压缩(使用zlib) * @param [type] $serializetype */ public function set($key,$value,$expire=0,$flag=0,$serializetype=null){ if($serializetype == 'json' && is_array($value)){ $value = json_encode($value); } $this->memcache->set($key,$value,$flag,$expire); } /** * 从服务端查找元素 * @param [type] $key * @return [type] */ public function get($key){ return $this->memcache->get($key); } /** * 增加一个条目到缓存服务器 * @param [type] $key * @param [type] $value * @param integer $expire * @param integer $flag * @param [type] $serializetype */ public function add($key,$value,$expire=0,$flag=0,$serializetype=null){ if($serializetype == 'json' && is_array($value)){ $value = json_encode($value); } $ret = $this->memcache->add($key,$value,$flag,$expire); return $ret; } /** * 清洗(删除)已经存储的所有的元素 * @return [type] */ public function flush(){ return $this->memcache->flush(); } /** * 从服务端删除一个元素 * @param [type] delete 参数:key要删除的元素的key 删除该元素的执行时间 timeout如果值为0,则该元素立即删除。 * @return [type] */ public function delete($key){ $ret = $this->memcache->delete($key,0); return $ret; } }
然后调用dlufmemcache:
1 $memcache = new dlufMemcache('127.0.0.1',11211); 2 $memcache->set('memcache','come on dluf&baidu !!!!!!'); 3 $ret = $memcache->get('memcache'); 4 echo print_r($ret,true);
运行输出可见:
想了解更多关于PHP的知识不?那就赶紧去关注PHP中文网的PHP视频教程吧!
以上是PHP调用MEMCACHE高速缓存技术实例的详细内容。更多信息请关注PHP中文网其他相关文章!

负载均衡会影响会话管理,但可以通过会话复制、会话粘性和集中式会话存储解决。1.会话复制在服务器间复制会话数据。2.会话粘性将用户请求定向到同一服务器。3.集中式会话存储使用独立服务器如Redis存储会话数据,确保数据共享。

Sessionlockingisatechniqueusedtoensureauser'ssessionremainsexclusivetooneuseratatime.Itiscrucialforpreventingdatacorruptionandsecuritybreachesinmulti-userapplications.Sessionlockingisimplementedusingserver-sidelockingmechanisms,suchasReentrantLockinJ

PHP会话的替代方案包括Cookies、Token-basedAuthentication、Database-basedSessions和Redis/Memcached。1.Cookies通过在客户端存储数据来管理会话,简单但安全性低。2.Token-basedAuthentication使用令牌验证用户,安全性高但需额外逻辑。3.Database-basedSessions将数据存储在数据库中,扩展性好但可能影响性能。4.Redis/Memcached使用分布式缓存提高性能和扩展性,但需额外配

Sessionhijacking是指攻击者通过获取用户的sessionID来冒充用户。防范方法包括:1)使用HTTPS加密通信;2)验证sessionID的来源;3)使用安全的sessionID生成算法;4)定期更新sessionID。

本文比较了PHP和ASP.NET,重点是它们对大规模Web应用程序,性能差异和安全功能的适用性。两者对于大型项目都是可行的,但是PHP是开源和无关的,而ASP.NET,


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

适用于 Eclipse 的 SAP NetWeaver 服务器适配器
将Eclipse与SAP NetWeaver应用服务器集成。

Atom编辑器mac版下载
最流行的的开源编辑器

SecLists
SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。

SublimeText3 Linux新版
SublimeText3 Linux最新版