zoukankan      html  css  js  c++  java
  • redis 基础配置

    基于spring boot

      第一步:添加pom文件依赖:

         

         <!-- redis -->
            <dependency>
                <groupId>redis.clients</groupId>
                <artifactId>jedis</artifactId>
                <version>2.9.0</version>
            </dependency>

     第二步:配置application文件
       
      
    jedis :  
      pool :  
        host : 127.0.0.1  
        port : 6379  
        config :  
          maxTotal: 100  
          maxIdle: 10  
          maxWaitMillis : 100000
    

      第三步:读取配置

    import org.springframework.beans.factory.annotation.Autowired;  
    import org.springframework.beans.factory.annotation.Qualifier;  
    import org.springframework.beans.factory.annotation.Value;  
    import org.springframework.context.annotation.Bean;  
    import org.springframework.context.annotation.Configuration;  
      
    import redis.clients.jedis.JedisPool;  
    import redis.clients.jedis.JedisPoolConfig;  
      
    @Configuration  
    public class RedisConfiguration {  
          
        @Bean(name= "jedis.pool")  
        @Autowired  
        public JedisPool jedisPool(@Qualifier("jedis.pool.config") JedisPoolConfig config,   
                    @Value("${jedis.pool.host}")String host,   
                    @Value("${jedis.pool.port}")int port) {  
            return new JedisPool(config, host, port);  
        }  
          
        @Bean(name= "jedis.pool.config")  
        public JedisPoolConfig jedisPoolConfig (@Value("${jedis.pool.config.maxTotal}")int maxTotal,  
                                    @Value("${jedis.pool.config.maxIdle}")int maxIdle,  
                                    @Value("${jedis.pool.config.maxWaitMillis}")int maxWaitMillis) {  
            JedisPoolConfig config = new JedisPoolConfig();  
            config.setMaxTotal(maxTotal);  
            config.setMaxIdle(maxIdle);  
            config.setMaxWaitMillis(maxWaitMillis);  
            return config;  
        }  
          
    }

    第四步:调用工具类demo

    import java.util.HashSet;
    import java.util.Set;
    
    import org.springframework.beans.factory.annotation.Autowired;  
    import org.springframework.stereotype.Component;  
      
    import redis.clients.jedis.Jedis;  
    import redis.clients.jedis.JedisPool;  
      
    /** 
     *  
     * @author Zhangyf
     * @param <T>
     * 
     */  
    @Component  
    public class RedisClient<T> {  
      
        @Autowired  
        private JedisPool jedisPool;  
          
        public void set(String key, String value) throws Exception {  
            Jedis jedis = null;  
            try {  
                jedis = jedisPool.getResource();  
                jedis.set(key, value);  
            } finally {  
                //返还到连接池  
                jedis.close();  
            }  
        }  
          
        public String get(String key) throws Exception  {  
      
            Jedis jedis = null;  
            try {  
                jedis = jedisPool.getResource();  
                return jedis.get(key);  
            } finally {  
                //返还到连接池  
                jedis.close();  
            }  
        }  
          
        public void setobj(String key, T value) throws Exception {  
            Jedis jedis = null;  
            try {  
                Set<T> set = new HashSet<T>();
                set.add(value);
                jedis = jedisPool.getResource();  
                jedis.sadd(key, String.valueOf(set));
            } finally {  
                //返还到连接池  
                jedis.close();  
            }  
        }
    }

    第五步:存取demo:

    import java.util.Map;
    
    import javax.servlet.http.HttpServletRequest;
    
    import org.apache.log4j.Logger;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.RequestBody;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.ResponseBody;
    import org.springframework.web.bind.annotation.RestController;
    
    import com.alibaba.fastjson.JSON;
    
    import yxm.zyf.love.domian.RoomDO;
    import yxm.zyf.love.mapper.RoomMapper;
    import yxm.zyf.love.redistool.RedisClient;
    import yxm.zyf.love.utils.LoggerUtil;
    import yxm.zyf.love.utils.StringUtil;
    import yxm.zyf.love.utils.URLencord;
    import yxm.zyf.love.vo.payVo;
    
    @RestController
    public class hospitalpay {
        private final static Logger logger = Logger.getLogger(hospitalpay.class);//给类初始化日志对象
    
        @Autowired
        private RoomMapper roomMapper;
    
        @Autowired
        private RedisClient redisClinet;
    
        @ResponseBody
        @RequestMapping(value = "room")
        public RoomDO room() {
            RoomDO roomdo = null;
            try {
                roomdo = JSON.parseObject(redisClinet.get("room"), RoomDO.class);
                if (roomdo == null) {
                    roomdo = roomMapper.selectByPrimaryKey(1);
                    redisClinet.set("room", JSON.toJSONString(roomdo));
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            LoggerUtil.info(logger, "出参roomdo:{0}", roomdo);//打印info级别的日志
            System.out.println(roomdo);
            return roomdo;
        }
    }
    

      



  • 相关阅读:
    ASP.NET MVC 常用内置验证特性 简介
    为什么要有get方法?-多态内存分析的应用
    多态的内存分析-转载
    抽象类和接口
    多态
    通过序列化复制整个对象
    检查异常和非检查异常
    怎样抛出自己定义的异常
    try catch和throws怎么用?-结合使用
    两个对象整体内容的比较-重写对象的equals方法
  • 原文地址:https://www.cnblogs.com/tianma-0/p/13439780.html
Copyright © 2011-2022 走看看