实战SSM_O2O商铺_47【Redis缓存】清除缓存接口的开发

版权声明:【show me the code ,change the world】 https://blog.csdn.net/yangshangwei/article/details/81750084

概述

设计如下: 在接口层传入缓存key的前缀,通过匹配的方式将能匹配到该前缀的所有key均删除。

举个例子
这里写图片描述

如上3个key,当我们传入shopcategory这个前缀时,会将如上3个前缀全部清除掉。


接口层改造

为了方便使用前缀,我们在将之前定义在方法体中的前缀抽取到接口层,如下所示

public interface AreaService {

    // redis key的前缀,抽取到接口层,方便使用
    public static final String AREALISTKEY = "arealist";

    .....
    .....   
}
public interface HeadLineService {

    // redis key的前缀,抽取到接口层,方便使用
    public static final String HEADLINEKEY = "headline";
    .....
    .....   
}
public interface ShopCategoryService {

    // redis key的前缀,抽取到接口层,方便使用
    public static final String SCLISTKEY = "shopcategory";

    .....
    .....   
}

CacheService接口

package com.artisan.o2o.service;

public interface CacheService {


    /**
     * 
     * 
     * @Title: removeFromCache
     * 
     * @Description: 根据缓存的前缀清理匹配的全部缓存
     * 
     * @param keyPrefix
     * 
     * @return: void
     */
    void removeFromCache(String keyPrefix);
}

CacheService接口实现类

package com.artisan.o2o.service.impl;

import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.artisan.o2o.cache.JedisUtil;
import com.artisan.o2o.service.CacheService;

@Service
public class CacheServiceImpl implements CacheService {

    @Autowired
    JedisUtil.Keys jedisKeys;

    @Override
    public void removeFromCache(String keyPrefix) {
        Set<String> keySet = jedisKeys.keys(keyPrefix + "*");
        for (String key : keySet) {
            jedisKeys.del(key);
        }
    }

}

工具类中的方法

       /**
         * 查找所有匹配给定的模式的键
         * 
         * @param String
         *            key的表达式,*表示多个,?表示一个
         * */
        public Set<String> keys(String pattern) {
            Jedis jedis = getJedis();
            Set<String> set = jedis.keys(pattern);
            jedis.close();
            return set;
        }


        /**
         * 删除keys对应的记录,可以是多个key
         * 
         * @param String
         *            ... keys
         * @return 删除的记录数
         * */
        public long del(String... keys) {
            Jedis jedis = getJedis();
            long count = jedis.del(keys);
            jedis.close();
            return count;
        }


单元测试

package com.artisan.o2o.service;

import java.io.IOException;
import java.util.List;

import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import com.artisan.o2o.BaseTest;
import com.artisan.o2o.entity.Area;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;

public class AreaServiceTest extends BaseTest {

    @Autowired
    AreaService areaService;

    @Autowired
    CacheService cacheService;

    @Test
    public void testGetAreaList() throws JsonParseException, JsonMappingException, IOException {
        // 首次从db中加载
        List<Area> areaList = areaService.getAreaList();
        for (Area area : areaList) {
            System.out.println("||---->" + area.toString());
        }

        // 再次查询从redis中获取
        areaList = areaService.getAreaList();
        for (Area area : areaList) {
            System.out.println("**---->" + area.toString());
        }
        // 清除缓存
        cacheService.removeFromCache(AreaService.AREALISTKEY);

        // 再次查询 从db中获取
        areaList = areaService.getAreaList();
        for (Area area : areaList) {
            System.out.println("**---->" + area.toString());
        }

        // 再次查询从redis中获取
        areaList = areaService.getAreaList();
        for (Area area : areaList) {
            System.out.println("**---->" + area.toString());
        }
    }

}

观察数据,确保测试结果符合预期。 比较简单就不贴数据了。


Github地址

代码地址: https://github.com/yangshangwei/o2o

猜你喜欢

转载自blog.csdn.net/yangshangwei/article/details/81750084
今日推荐