• SpringBoot(十二)-- 整合Redis


    1.pom依赖

        <!-- 添加redis支持 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>

    2.application.properties配置

    #redis数据库名称  从0到15,默认为db0  
    spring.redis.database=1
    #redis服务器名称  
    spring.redis.host=127.0.0.1
    #redis服务器密码  
    #spring.redis.password=123456
    #redis服务器连接端口号  
    spring.redis.port=6379
    #redis连接池设置  
    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.sentinel.master=  
    #spring.redis.sentinel.nodes=  
    spring.redis.timeout=60000

    3.将 五种数据类型 注入到 Srping中

    package com.xsjt.redis;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.data.redis.connection.RedisConnectionFactory;
    import org.springframework.data.redis.core.HashOperations;
    import org.springframework.data.redis.core.ListOperations;
    import org.springframework.data.redis.core.RedisTemplate;
    import org.springframework.data.redis.core.SetOperations;
    import org.springframework.data.redis.core.ValueOperations;
    import org.springframework.data.redis.core.ZSetOperations;
    import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
    import org.springframework.data.redis.serializer.StringRedisSerializer;
    /**
     * ClassName:RedisConfig Date: 2017年11月14日 下午3:39:34
     * 将 五种数据类型 注入到 Spring容器中
     * @author Joe
     * @version
     * @since JDK 1.8
     * 参考地址:https://www.cnblogs.com/skyessay/p/6485187.html
     */
    @Configuration
    public class RedisConfig {
        
        // 注入 RedisConnectionFactory
        @Autowired
        private RedisConnectionFactory redisConnectionFactory;
    
        @Bean
        public RedisTemplate<String, Object> functionDomainRedisTemplate() {
            RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
            initDomainRedisTemplate(redisTemplate, redisConnectionFactory);
            return redisTemplate;
        }
    
        /**
         * 设置数据存入 redis 的序列化方式
         * @param redisTemplate
         * @param factory
         */
        private void initDomainRedisTemplate(RedisTemplate<String, Object> redisTemplate, RedisConnectionFactory factory) {
            redisTemplate.setKeySerializer(new StringRedisSerializer());
            redisTemplate.setHashKeySerializer(new StringRedisSerializer());
            redisTemplate.setHashValueSerializer(new JdkSerializationRedisSerializer());
            redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer());
            redisTemplate.setConnectionFactory(factory);
        }
        
         /**
         * 实例化 HashOperations 对象,可以使用 Hash 类型操作
         * @param redisTemplate
         * @return
         */
        @Bean
        public HashOperations<String, String, Object> hashOperations(RedisTemplate<String, Object> redisTemplate) {
            return redisTemplate.opsForHash();
        }
    
        /**
         * 实例化 ValueOperations 对象,可以使用 String 操作
         * @param redisTemplate
         * @return
         */
        @Bean
        public ValueOperations<String, Object> valueOperations(RedisTemplate<String, Object> redisTemplate) {
            return redisTemplate.opsForValue();
        }
    
        /**
         * 实例化 ListOperations 对象,可以使用 List 操作
         * @param redisTemplate
         * @return
         */
        @Bean
        public ListOperations<String, Object> listOperations(RedisTemplate<String, Object> redisTemplate) {
            return redisTemplate.opsForList();
        }
    
        /**
         * 实例化 SetOperations 对象,可以使用 Set 操作
         * @param redisTemplate
         * @return
         */
        @Bean
        public SetOperations<String, Object> setOperations(RedisTemplate<String, Object> redisTemplate) {
            return redisTemplate.opsForSet();
        }
        
        /**
         * 实例化 ZSetOperations 对象,可以使用 ZSet 操作
         * @param redisTemplate
         * @return
         */
        @Bean
        public ZSetOperations<String, Object> zSetOperations(RedisTemplate<String, Object> redisTemplate) {
            return redisTemplate.opsForZSet();
        }
    }

    4.封装String数据类型的方法

    package com.xsjt.redis;  
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.data.redis.core.ValueOperations;
    import org.springframework.stereotype.Component;
    /**
     * ClassName: StringRedisUtil
     * String 数据类型
     * date: 2017年11月14日 下午8:15:07
     * @author Joe  
     * @version   
     * @since JDK 1.8
     */
    @Component("stringRedis")
    public class StringRedisUtil {
    
        @Autowired
        private ValueOperations<String, Object> redisTemplate;
        
        /**
         * set:(保存数据).  
         * @author Joe
         * Date:2017年11月14日下午8:15:01
         * @param key
         * @param value
         */
        public void set(String key, String value){
            redisTemplate.set(key, value);
        }
        
        /**
         * get:(得到数据).  
         * @author Joe
         * Date:2017年11月14日下午8:15:38
         * @param key
         * @return
         */
        public Object get(String key) {
            return redisTemplate.get(key);
        }
        
        // 可自行扩展其他方法
    }

    5.封装Hash数据类型的方法

    package com.xsjt.redis;
    import java.util.List;
    import java.util.Set;
    import java.util.concurrent.TimeUnit;
    import javax.annotation.Resource;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.data.redis.core.HashOperations;
    import org.springframework.data.redis.core.RedisTemplate;
    import org.springframework.stereotype.Component;
    
    /**
     * ClassName:HashRedisUtil Date: 2017年11月14日 下午8:17:47
     * Hash 数据类型
     * @author Joe
     * @version
     * @param <T>
     * @since JDK 1.8
     */
    @Component("hashRedis")
    public class HashRedisUtil<T> {
    
        @Autowired
        protected RedisTemplate<String, Object> redisTemplate;
        @Resource
        protected HashOperations<String, String, Object> hashOperations;
        
        /**
         * put:(添加).  
         * @param key
         * @param hashKey
         * @param doamin value
         * @param expire 过期时间(单位:秒),传入 -1 时表示不设置过期时间
         */
        public void put(String key, String hashKey, T doamin, long expire) {
            hashOperations.put(key, hashKey, doamin);
            if (expire != -1) {
                redisTemplate.expire(key, expire, TimeUnit.SECONDS);
            }
        }
    
        /**
         * remove:( 删除).  
         * @param key
         * @param hashKey
         */
        public void remove(String key, String hashKey) {
            hashOperations.delete(key, hashKey);
        }
    
        /**
         * get:(查询).  
         * @param key
         * @param hashKey
         * @return
         */
        public Object get(String key, String hashKey) {
            return hashOperations.get(key, hashKey);
        }
    
        /**
         * getAll:(获取当前redis库下所有对象).  
         * @param key
         * @return
         */
        public List<Object> getAll(String key) {
            return hashOperations.values(key);
        }
    
        /**
         * getKeys:(查询查询当前redis库下所有key).  
         * @param key
         * @return
         */
        public Set<String> getKeys(String key) {
            return hashOperations.keys(key);
        }
    
        /**
         * isKeyExists:(判断key是否存在redis中).  
         * @param key
         * @param hashKey
         * @return
         */
        public boolean isKeyExists(String key, String hashKey) {
            return hashOperations.hasKey(key, hashKey);
        }
    
        /**
         * count:(查询当前key下缓存数量).  
         * @param key
         * @return
         */
        public long count(String key) {
            return hashOperations.size(key);
        }
    }
    View Code

     6.测试类

    package com.xsjt.redis;
    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;
    /**  
     * ClassName:TestRedis 
     * Date:     2017年11月14日 下午8:09:54
     * @author   Joe  
     * @version    
     * @since    JDK 1.8
     */
    @RunWith(SpringRunner.class)
    @SpringBootTest
    public class TestRedis {
        
        /********************************测试String***********************************/
        
        @Autowired
        private StringRedisUtil stringRedis;
        
        @Test
        public void setString() {
            stringRedis.set("name", "张三");
        }
        
        @Test
        public void getString() {
            Object value = stringRedis.get("name");
            System.out.println("value=" + value);
        }
        
        /**********************************测试Hash************************************/
        
        @Autowired
        private HashRedisUtil<Object> hashRedisUtil;
        
        @Test
        public void setHash() {
            hashRedisUtil.put("user", "userName",  new Integer(6868), 5);
        }
        
        @Test
        public void getHash() {
            Integer a = (Integer) hashRedisUtil.get("user", "userName");
            System.out.println("a==" + a);
        }
    }

    7.源码下载

      https://gitee.com/xbq168/spring-boot-learn

  • 相关阅读:
    探讨游戏服务器设计
    找规律 0 1 3 8 22 64
    mysql 字段对比工具
    游戏开发者网站大集合
    sizeof struct 问题
    微软智力题
    python+requests——读取二进制文件并保存在本地——一个图片作为示例
    python+requests——检查响应头是否存在
    python+requests——读取二进制文件并保存在本地——一个应用程序作为示例
    python+requests——URL的编码和解码
  • 原文地址:https://www.cnblogs.com/xbq8080/p/7867939.html
Copyright © 2020-2023  润新知