zoukankan      html  css  js  c++  java
  • redis第二节

    一.java操作redis

      1.引入依赖

    <!-- https://mvnrepository.com/artifact/redis.clients/jedis -->
        <dependency>
          <groupId>redis.clients</groupId>
          <artifactId>jedis</artifactId>
          <version>2.9.0</version>
        </dependency>

      2.进行代码的编写

    package com.jedis;
    
    import org.junit.Before;
    import org.junit.Test;
    import redis.clients.jedis.Jedis;
    
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    import java.util.Set;
    
    public class redisAPI {
        private Jedis jedis;
    
        @Before
        public void before(){
            //连接jedis
            jedis=new Jedis("192.168.147.88");
            //设置密码
            jedis.auth("redis");
            jedis.ping();
            System.out.println("成功!!!");
        }
        @Test
        public void open() {
            //设置一个值
            jedis.set("zjn01","value01");
            //切快数据库
            jedis.select(1);
            //设置一个String类型的key- value
            jedis . set("strKey01" ,"strVa1ue01");
            //如何设置key的有效期
            //jedis. expire("strKey01”, 20);
            //获取String数据
            String value = jedis . get("strKey01");
            System.out.println("对应存放的va1ue值为: "+value);
            //修改数据
            jedis.set( "strKey01" , "strValue02");
            //删除数据
            jedis.del("strKey01");
            //设置多个值
            jedis.mset("strKey01","strVa1ue01","strKey02" ,"strValue02","strKey03","strVa1ue03","strKey04","1");
            //+1操作
            jedis . incr("strKey04");
            //-1操作
            jedis.decr("strKey04");
            //拼接
            jedis.append("strKey04","append");
        }
    
        @Test
        public void operationList(){
            //清空集合值
            jedis.del("listkey01","listkey02");
            //设置List集合值,左侧插入
            jedis. lpush("listkey01","张三","李四","王五");
            //设置List集合值,右侧插入
            jedis. rpush("listkey02","北京","上海", "广州");
            //获取List集合值
            List<String> listkey01 = jedis . lrange("listkey01",0,-1);
            for (String item: listkey01){
                System.out.println(item);
            }
            //更改List集合数据
            jedis.lset("listkey01",0, "赵六");
        }
    
        @Test
        public void operationSet(){
            //设置Set集合
            jedis . sadd("setkey01" ,"张三" ,"李四","王五");
            //获取
            Set<String> setkey01 = jedis . smembers("setkey01");
            for(String item:setkey01 ){
                System. out. println(item);
                }
            }
        @Test
        public void operationSortedSet() {
            //设置SortedSet集合
            jedis.zadd("setKey02", 1, "北京");
            jedis.zadd("setKey02", 2, "上海");
            //获取SortedSet
            Set<String> setKey02 = jedis.zrange("setKey02", 0, -1);
            for (String item : setKey02) {
                System.out.println(item);
            }
        }
        @Test
        public void operatioMap(){
            //设置Map
            Map<String,String> map=new
                    HashMap<>();
            map.put("name" ,"张三");
            map.put("age' ",
                    "18");
            jedis.hmset("mapkeye1" ,map);
            //获取值
            List<String> value = jedis.hmget("mapkeye1","name","age");
            for(String item:value){
                System.out.println(item);
            }
            Map<String, String> mopRels=jedis.hgetAll("mapkey01");
        }
    
    
    
    }

      3.查看效果

     二.SpringBoot集成Redis

      1.引入依赖

    <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>1.5.3.RELEASE</version>
    </parent>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-redis</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-redis</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
        </dependencies>

      2.增加配置文件

    ########################################################
    ###Redis (RedisConfiguration)
    ########################################################
    spring.redis.database=0
    spring.redis.host=127.0.0.1
    spring.redis.port=6379
    spring.redis.password=123456
    spring.redis.pool.max-idle=8
    spring.redis.pool.min-idle=0
    spring.redis.pool.max-active=8
    spring.redis.pool.max-wait=-1
    spring.redis.timeout=5000

      3.java代码

    @Service
    public class RedisService {
        @Autowired
        private StringRedisTemplate stringRedisTemplate;
    
        public void setObject(String key, Object value) {
            this.setObject(key, value, null);
        }
    
        public void setObject(String key, Object value, Long time) {
            if (StringUtils.isEmpty(key) || value == null) {
                return;
            }
            if (value instanceof String) {
                // 存放string类型
                String stringValue = (String) value;
                if (time == null) {
                    stringRedisTemplate.opsForValue().set(key, stringValue);
                } else {
                    stringRedisTemplate.opsForValue().set(key, stringValue, time, TimeUnit.SECONDS);
                }
    
                return;
            }
            if (value instanceof List) {
                // 存放list類型
                List<String> listValue = (List<String>) value;
                for (String string : listValue) {
                    stringRedisTemplate.opsForList().leftPush(key, string);
                }
    
            }
    
        }
    
        public void delKey(String key) {
            stringRedisTemplate.delete(key);
        }
    
        public String getString(String key) {
            return stringRedisTemplate.opsForValue().get(key);
        }
    
    } 
  • 相关阅读:
    程序模块化 与 单元测试
    【集美大学1411_助教博客】2017软件工程开跑啦。。。。
    沟通很重要
    参赛感言
    助教日志_期末总结
    助教日志_【沈阳航空航天大学软件工程 1,2班】期末排行
    [数据库事务与锁]详解一: 彻底理解数据库事务
    [数据库事务与锁]详解二: 数据库的读现象浅析
    [数据库事务与锁]详解三: 深入分析事务的隔离级别
    [数据库事务与锁]详解四: 数据库的锁机制
  • 原文地址:https://www.cnblogs.com/ws1149939228/p/12119129.html
Copyright © 2011-2022 走看看