1. 程式人生 > 實用技巧 >Java判斷一個時間是否在時間區間內

Java判斷一個時間是否在時間區間內

一丶新建dao類判斷redis是否失效

package com.ego.redis.dao.impl;

import javax.annotation.Resource;

import org.springframework.stereotype.Repository;

import com.ego.redis.dao.JedisDao;

import redis.clients.jedis.JedisCluster;
@Repository
public class JedisDaoImpl implements JedisDao{
    @Resource
    private JedisCluster jedisClients;
@Override public Boolean exists(String key) { return jedisClients.exists(key); } @Override public Long del(String key) { return jedisClients.del(key); } @Override public String set(String key, String value) { return jedisClients.set(key, value);
} @Override public String get(String key) { return jedisClients.get(key); } }

二丶使用

package com.ego.portal.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

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

import com.alibaba.dubbo.config.annotation.Reference;
import com.ego.commons.utils.JsonUtils;
import com.ego.dubbo.service.TbContentDubboService;
import com.ego.pojo.TbContent;
import com.ego.portal.service.TbContentService;
import com.ego.redis.dao.JedisDao;

@Service
public class TbContentServiceImpl implements TbContentService {
    @Reference
    private TbContentDubboService tbContentDubboServiceImpl;
    @Resource
    private JedisDao JedisDaoImpl;
    @Value("${redis.bigpic.key}")
    private String key;
    @Override
    public String showBigPic() {
        //先判斷在redis中是否存在
        if(JedisDaoImpl.exists(key)
){ String value = JedisDaoImpl.get(key); if(value!=null&&!value.equals("")){ return value; } } //如果不存在從mysql中取,取完後放入redis中 List
<TbContent> list = tbContentDubboServiceImpl.selByCount(6, true); List<Map<String,Object>> listResult = new ArrayList<>(); for (TbContent tc : list) { Map<String,Object> map = new HashMap<>(); map.put("srcB", tc.getPic2()); map.put("height", 240); map.put("alt", "對不起,載入圖片失敗"); map.put("width", 670); map.put("src", tc.getPic()); map.put("widthB", 550); map.put("href", tc.getUrl() ); map.put("heightB", 240); listResult.add(map); } String listJson = JsonUtils.objectToJson(listResult); //把資料放入到redis中 JedisDaoImpl.set(key, listJson); return listJson; } }