spring整合哨兵

redis.properties

redis.maxTotal=1000
redis.sentinel=192.168.161.135:26379

applicationContext-redis.xml


    
    
        
        
            
                ${redis.sentinel}
            
        
        
    
    
    
        
    

更改工具类的方法

package com.jt.common.service;

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

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisSentinelPool;

@Service
public class RedisService {

    @Autowired(required=false)
    private JedisSentinelPool sentinelPool;
    public void set(String key,String value){
        Jedis jedis=sentinelPool.getResource();
        jedis.set(key, value);
        sentinelPool.returnResource(jedis);
    }
    public String get(String key){
        Jedis jedis=sentinelPool.getResource();
        String result=jedis.get(key);
        sentinelPool.returnResource(jedis);
        return result;
    }
    public void set(String key,String value,int seconds){
        Jedis jedis=sentinelPool.getResource();
        jedis.setex(key,seconds,value);
        sentinelPool.returnResource(jedis);
    }
}

ItemCatServiceImpl

package com.jt.manage.service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

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

import com.alibaba.druid.util.StringUtils;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.jt.common.service.RedisService;
import com.jt.manage.mapper.ItemCatMapper;
import com.jt.manage.pojo.ItemCat;
import com.jt.manage.vo.EasyUITree;


@Service
public class ItemCatServiceImpl implements ItemCatService {
    @Autowired
    private ItemCatMapper itemCatMapper;

    @Autowired
    private RedisService redisService;

    private static ObjectMapper objectmapper = new ObjectMapper();

    /**
     * 1.根据parentId查询列表信息 2.将列表数据转化为volist集合
     */
    @Override
    public List findItemCatById(Long parentId) {
        ItemCat itemCat = new ItemCat();
        itemCat.setParentId(parentId);
        List itemCatList = itemCatMapper.select(itemCat);
        List treeList = new ArrayList();
        for (ItemCat itemCatTemp : itemCatList) {
            EasyUITree easyUITree = new EasyUITree();
            easyUITree.setId(itemCatTemp.getId());
            easyUITree.setText(itemCatTemp.getName());
            // 如果是父级则应该closed,如果不是父级则open
            String state = itemCatTemp.getIsParent() ? "closed" : "open";
            easyUITree.setState(state);
            treeList.add(easyUITree);
        }
        return treeList;
    }

    /**
     * 通过Redis缓存实现信息查询
     */
    @Override
    public List findCacheItemCatById(Long parentId) {
        List treeList = null;
        
        try {
            // 1.先查询缓存数据
            String key = "ITEM_CAT_" + parentId;
            String resultJSON = redisService.get(key);
            // 2.判断缓存中是否有数据
            if (StringUtils.isEmpty(resultJSON)) {
                // 2.1表示缓存中没有数据,需要查询后台数据库
                treeList = findItemCatById(parentId);
                // 2.2将java对象转化为JSON串
                String jsonData = objectmapper.writeValueAsString(treeList);
                // 2.3将数据写入缓存
                redisService.set(key, jsonData,30);
                System.out.println("第一次查询数据库");
            }else{
                //3缓存中有数据
                //3.1将JSON串转化为java对象(List)
                EasyUITree[] trees = objectmapper.readValue(resultJSON, EasyUITree[].class);
                treeList=Arrays.asList(trees);
                System.out.println("查询缓存中的数据");
            }
        } catch (Exception e) {
            treeList=findItemCatById(parentId);
        }
        return treeList;
    }
}

你可能感兴趣的:(spring整合哨兵)