zoukankan      html  css  js  c++  java
  • 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版本进行安装。

    
    
  • 相关阅读:
    sql2000如何完美压缩.mdf文件
    SQL Server读懂语句运行的统计信息 SET STATISTICS TIME IO PROFILE ON
    Sql Server性能优化辅助指标SET STATISTICS TIME ON和SET STATISTICS IO ON
    理解性能的奥秘——应用程序中慢,SSMS中快(6)——SQL Server如何编译动态SQL
    理解性能的奥秘——应用程序中慢,SSMS中快(5)——案例:如何应对参数嗅探
    理解性能的奥秘——应用程序中慢,SSMS中快(4)——收集解决参数嗅探问题的信息
    理解性能的奥秘——应用程序中慢,SSMS中快(3)——不总是参数嗅探的错
    理解性能的奥秘——应用程序中慢,SSMS中快(2)——SQL Server如何编译存储过程
    巧用Map缓存提升"翻译"速度
    pt-archiver配置自动归档
  • 原文地址:https://www.cnblogs.com/chenziyu/p/9225325.html
Copyright © 2011-2022 走看看