SpringDataRedis小案例

一。下載Redisjava

  1. 下載Redis鏡像mysql

[root@192 ~]# docker image pull registry.cn-hangzhou.aliyuncs.com/zuowenbo/redis
Using default tag: latest
[root@192 ~]# docker image tag registry.cn-hangzhou.aliyuncs.com/zuowenbo/redis redis
[root@192 ~]# docker image rm registry.cn-hangzhou.aliyuncs.com/zuowenbo/redis
[root@192 ~]# docker image list
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
dubbo-admin         latest              4b43bc0f997e        3 weeks ago         360MB
mysql               5.6                 27e29668a08a        7 weeks ago         256MB
redis               latest              ba6ae5a37f30        5 months ago        28.7MB
zookeeper           latest              64e049ee9478        6 months ago        148MB
fastdfs             latest              63ca62a9f44c        18 months ago       457MB
[root@192 ~]# 

  2. 運行Redis容器redis

[root@192 ~]# docker container run --name redis -p 6379:6379 -v /data/redis_data:/data -d redis redis-server --appendonly yes
a214a85d3dff88f05ab0422d898e1ba0b2db93db44455d8604ad2706dca917e4
[root@192 ~]# docker container list
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
a214a85d3dff        redis               "docker-entrypoint.s…"   21 seconds ago      Up 21 seconds       0.0.0.0:6379->6379/tcp   redis
[root@192 ~]# 

--name:容器名稱spring

-p:端口映射,冒號左邊爲主機端口sql

-v:主機文件夾掛載打容器,冒號左邊爲主機的docker

-d:後臺運行apache

redis-server --appendonly yes : 在容器執行redis-server啓動命令,並打開redis持久化配置mvc

[root@192 ~]# ls /data/redis_data/
appendonly.aof
[root@192 ~]#

二。配置案例app

  1. 新建SpringDataRedisDemo工程,並繼承父工程maven

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>parent</artifactId>
        <groupId>com.zgh</groupId>
        <version>1.0-SNAPSHOT</version>
        <relativePath>../parent/pom.xml</relativePath>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>SpringDataRedisDemo</artifactId>


</project>

  2. 配置redis-config.properties

# Redis settings 
# server IP 
redis.host=10.20.0.129
# server port 
redis.port=6379
# server pass 
redis.pass=
# use dbIndex 
redis.database=0

redis.maxIdle=300

redis.maxWait=3000

redis.testOnBorrow=true

  3. 配置applicationContext-redis.xml

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" 
  xmlns:context="http://www.springframework.org/schema/context" 
  xmlns:mvc="http://www.springframework.org/schema/mvc" 
  xmlns:cache="http://www.springframework.org/schema/cache"
  xsi:schemaLocation="http://www.springframework.org/schema/beans   
            http://www.springframework.org/schema/beans/spring-beans.xsd   
            http://www.springframework.org/schema/context   
            http://www.springframework.org/schema/context/spring-context.xsd   
            http://www.springframework.org/schema/mvc   
            http://www.springframework.org/schema/mvc/spring-mvc.xsd 
            http://www.springframework.org/schema/cache  
            http://www.springframework.org/schema/cache/spring-cache.xsd">  
  
   <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>  
      
</beans>  

三。新建案例

  1. 對值類型操做,新建JUnit測試:TestValue

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
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:spring/applicationContext-redis.xml")

public class TestValue {

    @Autowired
    private RedisTemplate redisTemplate;

    @Test
    public void setValue(){
        redisTemplate.boundValueOps("name").set("name1");
    }

    @Test
    public void getValue(){
        String str = (String) redisTemplate.boundValueOps("name").get();
        System.out.println(str);
    }

    @Test
    public void deleteValue(){
        redisTemplate.delete("name");
    }
}

執行setValue

執行getValue

執行deleteValue,再執行getValue沒數據了

 

  2. 對Set類型操做,新建JUnit測試:TestSet

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

import java.util.Set;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:spring/applicationContext-redis.xml")

public class TestSet {

    @Autowired
    private RedisTemplate redisTemplate;

    @Test
    public void setValue(){
        redisTemplate.boundSetOps("nameset").add("曹操");
        redisTemplate.boundSetOps("nameset").add("劉備");
        redisTemplate.boundSetOps("nameset").add("孫權");
    }

    @Test
    public void getValue(){
        Set members = redisTemplate.boundSetOps("nameset").members();
        System.out.println(members);
    }

    @Test
    public void deleteValue(){
        redisTemplate.boundSetOps("nameset").remove("孫權");
    }

    @Test
    public void deleteAllValue(){
        redisTemplate.delete("nameset");
    }
}

  3. 對List類型操做,新建JUnit測試:TestList

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

import java.util.List;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:spring/applicationContext-redis.xml")

public class TestList {

    @Autowired
    private RedisTemplate redisTemplate;

    @Test
    public void setValue(){
        /*右壓棧:後添加的對象排在後邊*/
        redisTemplate.boundListOps("namelist1").rightPush("劉備");
        redisTemplate.boundListOps("namelist1").rightPush("關羽");
        redisTemplate.boundListOps("namelist1").rightPush("張飛");
    }

    @Test
    public void setValue1(){
        /*左壓棧:後添加的對象排在前邊*/
        redisTemplate.boundListOps("namelist2").leftPush("劉備");
        redisTemplate.boundListOps("namelist2").leftPush("關羽");
        redisTemplate.boundListOps("namelist2").leftPush("張飛");
    }

    @Test
    public void getValue(){
        /*顯示右壓棧集合*/
        List list = redisTemplate.boundListOps("namelist1").range(0, 10);
        System.out.println(list);
    }

    @Test
    public void getValue1(){
        /*顯示左壓棧集合*/
        List list = redisTemplate.boundListOps("namelist2").range(0, 10);
        System.out.println(list);
    }

    @Test
    public void testSearchByIndex(){
        /*查詢集合某個元素*/
        String s = (String) redisTemplate.boundListOps("namelist1").index(1);
        System.out.println(s);
    }

    @Test
    public void testRemoveByIndex() {
        /*移除集合某個元素*/
        redisTemplate.boundListOps("namelist1").remove(1, "關羽");
    }

    @Test
    public void deleteValue(){
        redisTemplate.delete("namelist2");
    }
}

  4. 對Hash類型操做,新建JUnit測試:TestHash

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

import java.util.List;
import java.util.Set;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:spring/applicationContext-redis.xml")

public class TestHash {

    @Autowired
    private RedisTemplate redisTemplate;

    @Test
    public void setValue(){
        redisTemplate.boundHashOps("namehash").put("a", "唐僧");
        redisTemplate.boundHashOps("namehash").put("b", "悟空");
        redisTemplate.boundHashOps("namehash").put("c", "八戒");
        redisTemplate.boundHashOps("namehash").put("d", "沙僧");
    }

    @Test
    public void testGetKeys(){
        Set s = redisTemplate.boundHashOps("namehash").keys();
        System.out.println(s);
    }

    @Test
    public void testGetValues(){
        List values = redisTemplate.boundHashOps("namehash").values();
        System.out.println(values);
    }

    @Test
    public void testGetValueByKey(){
        Object object = redisTemplate.boundHashOps("namehash").get("b");
        System.out.println(object);
    }

    @Test
    public void testRemoveValueByKey(){
        redisTemplate.boundHashOps("namehash").delete("c");
    }

    @Test
    public void deleteValue(){
        redisTemplate.delete("namehash");
    }
}
相關文章
相關標籤/搜索