• 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版本进行安装。

    
    
  • 相关阅读:
    BZOJ 4010: [HNOI2015]菜肴制作( 贪心 )
    bzoj 1084
    bzoj 2763
    bzoj 1003
    bzoj 1858
    codevs 1296
    cf 438D
    vijos 1083
    codevs 3303
    bzoj 1296
  • 原文地址:https://www.cnblogs.com/chenziyu/p/9225325.html
Copyright © 2020-2023  润新知