redis expireat在java中的使用

package testMaven2;

import java.io.UnsupportedEncodingException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:config/applicationContext.xml" })
public class CodeTest1 {
    @Autowired
    ApplicationContext applicationContext;
    @Autowired
    RedisTemplate<String, Object> redisTemplate;

    @SuppressWarnings("unchecked")
    @Test
    public void test() throws UnsupportedEncodingException, ParseException {
        redisTemplate = applicationContext.getBean(RedisTemplate.class);
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long time = dateFormat.parse("2018-08-06 12:41:00").getTime();
        Date date = new Date(time);
        redisTemplate.expireAt("key1", date);
        System.out.println(redisTemplate.hasKey("key1"));
    }
}
 

猜你喜欢

转载自blog.csdn.net/qq_34561892/article/details/81452342