zoukankan      html  css  js  c++  java
  • SpringDataRedis小案例

    一。下载Redis

      1. 下载Redis镜像

    [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容器

    [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:容器名称

    -p:端口映射,冒号左边为主机端口

    -v:主机文件夹挂载打容器,冒号左边为主机的

    -d:后台运行

    redis-server --appendonly yes : 在容器执行redis-server启动命令,并打开redis持久化配置

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

    二。配置案例

      1. 新建SpringDataRedisDemo工程,并继承父工程

    <?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");
        }
    }
  • 相关阅读:
    Ubuntu 安装 JDK 7 / JDK8 的两种方式
    python 深拷贝 浅拷贝 赋值
    importlib.import_module
    pandas分块读取大量数据集
    win10下安装XGBoost Gpu版本
    win10下安装LGBM GPU版本
    统计自然语言处理(第二版)笔记1
    K-近邻算法
    2019考研的一些心得
    lib和dll的区别与使用
  • 原文地址:https://www.cnblogs.com/GH-123/p/10391366.html
Copyright © 2011-2022 走看看