php缓存写法,编程技术缓存写法(一)

介绍

本篇主要说下平常项目中缓存使用经验和遇到过的问题。

目录

一: 基本写法

二:缓存雪崩

1:全局锁,实例锁

2:字符串锁

三:缓存穿透

四:再谈缓存雪崩

五:总结

一:基本写法

为了方便演示,我们用Runtime.Cache做缓存容器,并定义个简单操作类。如下:public class CacheHelper

{

public static object Get(string cacheKey)

{

return HttpRuntime.Cache[cacheKey];

}

public static void Add(string cacheKey, object obj, int cacheMinute)

{

HttpRuntime.Cache.Insert(cacheKey, obj, null, DateTime.Now.AddMinutes(cacheMinute),

Cache.NoSlidingExpiration, CacheItemPriority.Normal, null);

}

}

简单读取:public object GetMemberSigninDays1()

{

const int cacheTime = 5;

const string cacheKey = "mushroomsir";

var cacheValue = CacheHelper.Get(cacheKey);

if (cacheValue != null)

return cacheValue;

cacheValue = "395"; //这里一般是 sql查询数据。 例:395 签到天数

CacheHelper.Add(cacheKey, cacheValue, cacheTime);

return cacheValue;

}

在项目中,有不少这样写法。这样写没有错,但在并发量上来后就会有问题。继续看

二:缓存雪崩

缓存雪崩是由于缓存失效(过期),新缓存未到期间。

这个中间时间内,所有请求都去查询数据库,而对数据库CPU和内存造成巨大压力,前端连接数不够、查询阻塞。

这个中间时间并没有那么短,比如sql查询1秒,加上传输解析0.5秒。 就是说1.5秒内所有用户查询,都是直接查询数据库的。

这种情况下,我们想到最多的就是加锁排队了。

1:全局锁,实例锁public static object obj1 = new object();

public object GetMemberSigninDays2()

{

const int cacheTime = 5;

const string cacheKey = "mushroomsir";

var cacheValue = CacheHelper.Get(cacheKey);

if (cacheValue != null)

return cacheValue;

//lock (obj1) //全局锁

//{

// cacheValue = CacheHelper.Get(cacheKey);

// if (cacheValue != null)

// return cacheValue;

// cacheValue = "395"; //这里一般是 sql查询数据。 例:395 签到天数

// CacheHelper.Add(cacheKey, cacheValue, cacheTime);

//}

lock (this)

{

cacheValue = CacheHelper.Get(cacheKey);

if (cacheValue != null)

return cacheValue;

cacheValue = "395"; //这里一般是 sql查询数据。 例:395 签到天数

CacheHelper.Add(cacheKey, cacheValue, cacheTime);

}

return cacheValue;

}

第一种:lock (obj1) 是全局锁可以满足,但我们要为每个函数都声明一个obj,不然在A、B函数都锁obj1时,必然会让其中一个阻塞。

第二种:lock (this) 这个锁当前实例,对其他实例无效,这个锁就没什么效果了。使用单例模式的可以锁。

但在当前实例中:A函数锁当前实例,其他锁当前实例的函数读写,也被阻塞。 不可取

2:字符串锁

既然锁对象不行,利用字符串的特性,我们直接锁缓存key呢。来看下public object GetMemberSigninDays3()

{

const int cacheTime = 5;

const string cacheKey = "mushroomsir";

var cacheValue = CacheHelper.Get(cacheKey);

if (cacheValue != null)

return cacheValue;

const string lockKey = cacheKey + "n(*≧▽≦*)n";

//lock (cacheKey)

//{

// cacheValue = CacheHelper.Get(cacheKey);

// if (cacheValue != null)

// return cacheValue;

// cacheValue = "395"; //这里一般是 sql查询数据。 例:395 签到天数

// CacheHelper.Add(cacheKey, cacheValue, cacheTime);

//}

lock (lockKey)

{

cacheValue = CacheHelper.Get(cacheKey);

if (cacheValue != null)

return cacheValue;

cacheValue = "395"; //这里一般是 sql查询数据。 例:395 签到天数

CacheHelper.Add(cacheKey, cacheValue, cacheTime);

}

return cacheValue;

}

第一种:lock (cacheName) 有问题,因为字符串也是共享的,会阻塞其他使用这个字符串的操作行为。 具体请看之前的博文 c#语言-多线程中的锁系统(一)。

2015-01-04 13:36更新:因为字符串被公共语言运行库 (CLR)暂留,这意味着整个程序中任何给定字符串都只有一个实例。所以才会用第二种

第二种:lock (lockKey) 可以满足。其实目就是为了保证锁的粒度最小并且全局唯一性,只锁当前缓存的查询行为。

三:缓存穿透

举个简单例子:一般我们会缓存用户搜索结果。而数据库查询不到,是不会做缓存的。但如果频繁查这个关键字,就会每次都直查数据库了。

这样缓存就没意义了,这也是常提的缓存命中率问题。public object GetMemberSigninDays4()

{

const int cacheTime = 5;

const string cacheKey = "mushroomsir";

var cacheValue = CacheHelper.Get(cacheKey);

if (cacheValue != null)

return cacheValue;

const string lockKey = cacheKey + "n(*≧▽≦*)n";

lock (lockKey)

{

cacheValue = CacheHelper.Get(cacheKey);

if (cacheValue != null)

return cacheValue;

cacheValue = null; //数据库查询不到,为空。

//if (cacheValue2 == null)

//{

// return null; //一般为空,不做缓存

//}

if (cacheValue == null)

{

cacheValue = string.Empty; //如果发现为空,我设置个默认值,也缓存起来。

}

CacheHelper.Add(cacheKey, cacheValue, cacheTime);

}

return cacheValue;

}

例子中我们把查询不到的结果,也给缓存起来了。这样就可以避免,查询为空时,引起缓存穿透了。

当然我们也可以单独设置个缓存区,进行第一层控制校验。 以便和正常缓存区分开了。

四:再谈缓存雪崩

额 不是用加锁排队方式就解决了吗?其实加锁排队只是为了减轻DB压力,并没有提高系统吞吐量。

在高并发下: 缓存重建期间,你是锁着的,1000个请求999个都在阻塞的。 用户体验不好,还浪费资源:阻塞的线程本可以处理后续请求的。public object GetMemberSigninDays5()

{

const int cacheTime = 5;

const string cacheKey = "mushroomsir";

//缓存标记。

const string cacheSign = cacheKey + "_Sign";

var sign = CacheHelper.Get(cacheSign);

//获取缓存值

var cacheValue = CacheHelper.Get(cacheKey);

if (sign != null)

return cacheValue; //未过期,直接返回。

lock (cacheSign)

{

sign = CacheHelper.Get(cacheSign);

if (sign != null)

return cacheValue;

CacheHelper.Add(cacheSign, "1", cacheTime);

ThreadPool.QueueUserWorkItem((arg) =>

{

cacheValue = "395"; //这里一般是 sql查询数据。 例:395 签到天数

CacheHelper.Add(cacheKey, cacheValue, cacheTime*2); //日期设缓存时间的2倍,用于脏读。

});

}

return cacheValue;

}

代码中,我们多用个缓存标记key,双检锁校验。它设置为正常时间,过期后通知另外的线程去更新缓存数据。

而实际的缓存由于设置了2倍的时间,仍然可以能用脏数据给前端展现。

这样就能提高不少系统吞吐量了。

五:总结

补充下: 这里说的阻塞其他函数指的是,高并发下锁同一对象。

实际使用中,缓存层封装往往要复杂的多。 关于更新缓存,可以单开一个线程去专门跑这些,图方便就扔线程池吧。

具体使用场景,可根据实际用户量来平衡。

本文原创发布php中文网,转载请注明出处,感谢您的尊重!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值