springboot+JPA 整合redis

1.导入redis依赖:

 

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
  <version>2.0.3.RELEASE</version>
</dependency>

2.配置redis参数:

 

#redis配置
spring.redis.host=localhost
spring.redis.port=6379
spring.redis.database=1
spring.redis.jedis.pool.max-active=8
spring.redis.jedis.pool.max-wait= -1ms
spring.redis.jedis.pool.max-idle=500

3.编写redis工具类:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Repository;
import springbootd.demo.entity.User;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Repository
public class RedisUtil {

    @Autowired
    private StringRedisTemplate template;

    @Autowired
    private RedisTemplate<String,Object> redisTemplate;

    public void testSetKey(String key,Object value){
        ValueOperations<String,Object> ops =redisTemplate.opsForValue();
        ops.multiSet(value);
    }

    public User testGetValue(String key){
        ValueOperations<String,Object> ops =redisTemplate.opsForValue();
        return (User) ops.get(key);
    }

    public void setKey(String key ,String value){
        ValueOperations<String,String> ops = template.opsForValue();
        ops.set(key,value,1, TimeUnit.MINUTES);
    }

    public String getValue(String key){
        ValueOperations<String,String> ops = this.template.opsForValue();
        return ops.get(key);
    }
}

 

5.测试:

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import springbootd.demo.entity.User;
import springbootd.demo.util.RedisUtil;

@RunWith(SpringRunner.class)
@SpringBootTest
public class DemoApplicationTests {

   @Test
   public void contextLoads() {
   }

   @Autowired
   RedisUtil redisUtil;

   @Test
   public void testRedis(){
      redisUtil.setKey("userName","chen");
      redisUtil.setKey("age","18");


      User user = new User();
      user.setId((long) 2);
      user.setUserName("xiaomi");
      user.setPassWord("123");
      redisUtil.testSetKey("user_2",user);

      System.out.println(redisUtil.testGetValue("user_2").toString());

      System.out.println("用户名"+redisUtil.getValue("userName"));
      System.out.println("年龄"+redisUtil.getValue("age"));


   }
}

 redis如果要缓存实体类的话,此实体类必须序列化:

public class User implements Serializable{

}

 

或者使用gson工具进行实体类和json字符串之间转换:

1.gson依赖

<dependency>
   <groupId>com.google.code.gson</groupId>
   <artifactId>gson</artifactId>
   <version>2.8.4</version>
</dependency>

 

转换方法:

@Test
    public void testRedis(){

        Gson gson =new Gson();

        User user = new User();
        user.setId((long) 3);
        user.setUserName("xiaomi");
        user.setPassWord("123");
        redisUtil.testSetKey("user_3",gson.toJson(user));

        String userStr =redisUtil.testGetValue("user_3");

        System.out.println(gson.fromJson(userStr,User.class).toString());

    }

 

 

Redis官方没有提供Window版本,不过微软维护了一个版本,下载地址为:https://github.com/MSOpenTech/redis/releases,下载.msi版本进行安装。


posted @ 2018-06-25 17:37  晨子语  阅读(486)  评论(0编辑  收藏  举报