Spring Data Redis 框架入门学习及小Demo练习

1、简介

         Spring-data-redisspring大家族的一部分,提供了在spring应用中通过简单的配置访问redis服务,对reids底层开发包(Jedis,  JRedis, and RJC)进行了高度封装,RedisTemplate提供了redis各种操作、异常处理及序列化,支持发布订阅,并对spring 3.1 cache进行了实现。

2、功能

         spring-data-redis针对jedis提供了如下功能:
         1>连接池自动管理,提供了一个高度封装的“RedisTemplate”类
         2>针对jedis客户端中大量api进行了归类封装,将同一类型操作封装为operation接口
                 ValueOperations:简单K-V操作
                 SetOperations:set类型数据操作
                 ZSetOperations:zset类型数据操作
                 HashOperations:针对map类型的数据操作
                 ListOperations:针对list类型的数据操作

3、小demo练习

    1>准备工作

        (1)构建Maven工程  SpringDataRedisDemo

        (2)引入Spring相关依赖、引入JUnit依赖

        (3)引入JedisSpringDataRedis依赖

<!-- 缓存 -->

<dependency>

           <groupId>redis.clients</groupId>

           <artifactId>jedis</artifactId>

           <version>2.8.1</version>

</dependency>

<dependency>

           <groupId>org.springframework.data</groupId>

           <artifactId>spring-data-redis</artifactId>

           <version>1.7.2.RELEASE</version>

</dependency>   

      (4)创建properties文件夹,建立redis-config.properties

redis.host=127.0.0.1

redis.port=6379

redis.pass=

redis.database=0

redis.maxIdle=300

redis.maxWait=3000

redis.testOnBorrow=true

        (5)创建spring文件夹 ,创建applicationContext-redis.xml

   <context:property-placeholder location="classpath*:properties/*.properties" />  

   <!-- redis 相关配置 -->

   <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig"> 

     <property name="maxIdle" value="${redis.maxIdle}" />  

     <property name="maxWaitMillis" value="${redis.maxWait}" /> 

     <property name="testOnBorrow" value="${redis.testOnBorrow}" /> 

   </bean> 

   <bean id="JedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"

       p:host-name="${redis.host}" p:port="${redis.port}" p:password="${redis.pass}" p:pool-config-ref="poolConfig"/> 

  

   <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"> 

     <property name="connectionFactory" ref="JedisConnectionFactory" /> 

   </bean> 

2> hash类型的操作

创建测试类TestHash.java

(1)存入值

    @Test

    public void testSetValue(){

         redisTemplate.boundHashOps("namehash").put("a", "唐僧");

         redisTemplate.boundHashOps("namehash").put("b", "悟空");

         redisTemplate.boundHashOps("namehash").put("c", "八戒");

         redisTemplate.boundHashOps("namehash").put("d", "沙僧");

    }

(2)提取所有的KEY

    @Test

    public void testGetKeys(){

         Set s = redisTemplate.boundHashOps("namehash").keys();     

         System.out.println(s);       

    }

 

(3)提取所有的值

    @Test

    public void testGetValues(){

         List values = redisTemplate.boundHashOps("namehash").values();

         System.out.println(values);      

    }

(4)根据KEY提取值

    @Test

    public void testGetValueByKey(){

         Object object = redisTemplate.boundHashOps("namehash").get("b");

         System.out.println(object);

    }

(5)根据KEY移除值

    @Test

    public void testRemoveValueByKey(){

         redisTemplate.boundHashOps("namehash").delete("c");

    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是在 Ubuntu 上使用 Spring Boot 和 Redis 执行 Geo 相关操作的示例: 1. 安装 Redis 和 Java 在 Ubuntu 上安装 Redis 和 Java,可以使用以下命令: ``` sudo apt-get update sudo apt-get install redis-server sudo apt-get install default-jre ``` 2. 创建 Spring Boot 项目 使用以下命令创建一个 Spring Boot 项目: ``` spring init --dependencies=web,data-redis myproject ``` 这将创建一个名为 `myproject` 的 Spring Boot 项目,并添加 Redis 和 Web 依赖项。 3. 添加 Redis 配置 在 `src/main/resources` 目录下创建名为 `application.properties` 的文件,并添加以下 Redis 配置: ``` spring.redis.host=localhost spring.redis.port=6379 ``` 这将告诉 Spring Boot 使用本地 Redis 数据库。 4. 编写控制器 在 `src/main/java` 目录下创建一个名为 `MyController` 的控制器,并添加以下代码: ```java import org.springframework.data.redis.connection.RedisGeoCommands; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.geo.GeoLocation; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class MyController { private final RedisTemplate<String, String> redisTemplate; public MyController(RedisTemplate<String, String> redisTemplate) { this.redisTemplate = redisTemplate; } @GetMapping("/addGeo") public void addGeo() { redisTemplate.opsForGeo().add("mylocations", new GeoLocation<>("New York", new RedisGeoCommands.GeoLocation<>(-74.005974, 40.712776))); redisTemplate.opsForGeo().add("mylocations", new GeoLocation<>("Paris", new RedisGeoCommands.GeoLocation<>(2.352222, 48.856614))); } } ``` 控制器中的 `addGeo` 方法将使用 RedisTemplate 实例将两个地理位置添加到名为 `mylocations` 的 Redis Geo 集合中。 5. 运行应用程序 在终端中导航到项目目录并运行以下命令: ``` ./mvnw spring-boot:run ``` 这将启动应用程序并将其绑定到默认端口(8080)。 6. 测试应用程序 使用 curl 或浏览器访问 `http://localhost:8080/addGeo`,将两个地理位置添加到 Redis Geo 集合中。 可以使用以下命令在 Redis 命令行中查看集合的成员: ``` 127.0.0.1:6379> GEORADIUS mylocations 15 37.618423 55.751244 km ``` 这将返回距离(37.618423,55.751244)坐标 15 公里内的成员列表。 这就是在 Ubuntu 上使用 Spring Boot 和 Redis 执行 Geo 相关操作的简单示例。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值