码迷,mamicode.com
首页 > 系统相关 > 详细

memcache真实项目缓存实例

时间:2015-06-10 17:27:34      阅读:174      评论:0      收藏:0      [点我收藏+]

标签:

最近因为公司的项目数据量越来越大,导致在系统在运行的时候,每一次都进行大量的查询,很多页面的查询的频率很高,所以每次都进行了差不多一模一样的查询,返回了一模一样的数据,其实这样就非常的浪费资源了,所以就将一部分常用的数据,放在缓存中,如果下次还是查询相同的内容,那么就可以直接从缓存中返回了,不需要和数据库进行交互了。

公司的客户要频繁的查自己的报表数据,返回相同的数据频率很高,现在准备把报表的数据缓存到memcache的缓存中,因为报表是一个小时更新一次,所以我们缓存数据的时间也调整到一个小时。

思路:因为memcache是根据键值对(K-V)存储的,所以每次返回的报表数据通常都是10条,
根据查询条件的不同,返回的数据也是不同的
1.用查询条件和分页条件作为key,查询出所有的对象的主键id的集合作为value。
2.将1查询出来的所有对象,每个对象都把自己的主键id作为key,自身对象作为value缓存到memcache缓存中。

存储一次的结果可能是这样的:
(10_1_4352_9674_null , [85,48,69,345,849,279])
(85,object1)
(48,object2)
(69,object3)
(345,object4)
(849,object5)
(279,object6)

这样分开存储是有好处的,如果说只把把查询条件和分页条件作为key,查询出来的结果集合作为value的话,是很有局限性的。把查询出来的对象,通过id单独缓存的话这样很多结果集中间有重复的对象就不会在缓存第二次了。

来看一下缓存的代码.

private void cacheData(QueryResult<AdspacereportInfo> result,String queryStr,String cacheType) {
        List<AdspacereportInfo> resultList = result.getResultList();
        String totalRecord = result.getTotalRecord()+"";

        String cacheKey = cacheType+Constant.CACHE_ADSREPORT;
        try {
            ArrayList<String> idList = new ArrayList<String>();
            // 设置arraylist容器的初始容量
            idList.ensureCapacity(resultList.size());

            for(AdspacereportInfo report : resultList){
                idList.add(report.getId()+"");

                if(null != report){
                    // 1 缓存查询结果
                    if (null == memcachedClient.get(cacheKey+report.getId())) {
                        memcachedClient.set(cacheKey+report.getId(), MemcachedManager.CACHE_EXP_HALF_HOUR, report);
                    }
                }
            }

            // 2 缓存查询条件
            if( null == memcachedClient.get(cacheKey+queryStr)){
                memcachedClient.set(cacheKey+queryStr, MemcachedManager.CACHE_EXP_HALF_HOUR, idList);
            }

            // 3 缓存查询总数
            if( null == memcachedClient.get(cacheKey+Constant.CACHE_SIZE+queryStr)){
                memcachedClient.set(cacheKey+Constant.CACHE_SIZE+queryStr, MemcachedManager.CACHE_EXP_HALF_HOUR, totalRecord);
            }
        } catch (TimeoutException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (MemcachedException e) {
            e.printStackTrace();
        }

    }

这是查询缓存的代码:

/**
*  @queryStr  就是查询条件和分页条件
*/

private QueryResult<AdspacereportInfo> queryCacheData(String queryStr,String cacheType) {
        QueryResult<AdspacereportInfo> result = new QueryResult<AdspacereportInfo>();
        List<AdspacereportInfo> list = new ArrayList<AdspacereportInfo>();
        String cacheKey = cacheType+Constant.CACHE_ADSREPORT;
        try {
            // 1  查出只含有 id的集合{2, 38, 75, 8, 3, 5}
            ArrayList<String> arr =(ArrayList<String>) memcachedClient.get(cacheKey+queryStr);
            if(null != arr){
                for(String id : arr){
                    //  2   遍历id集合  取出对象
                    AdspacereportInfo report =(AdspacereportInfo) memcachedClient.get(cacheKey+id);
                    if( null != report){
                        list.add(report);
                    }
                }
            }

            // 3 查出缓存的数据大小
            String totalRecord =(String) memcachedClient.get(cacheKey+Constant.CACHE_SIZE+queryStr);

            //  4  组装
            result.setResultList(list);
            if(StringUtils.isNotBlank(totalRecord)){
                result.setTotalRecord(Integer.valueOf(totalRecord));
            } else {
                //  默认  10
                result.setTotalRecord(10);
            }

        } catch (TimeoutException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (MemcachedException e) {
            e.printStackTrace();
        }

        return result;
    }

memcache真实项目缓存实例

标签:

原文地址:http://blog.csdn.net/judyfun/article/details/46442933

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!