美文网首页
高并发扩容之缓存

高并发扩容之缓存

作者: 磊_5d71 | 来源:发表于2018-11-05 17:03 被阅读0次
    图片.png
    图片.png

    缓存

    图片.png
    图片.png 图片.png

    缓存 Guava Cache

    图片.png
    图片.png
    图片.png 图片.png
    • 学习redis网站 redis.cn

    • RedisConfig

    package com.alan.concurrency.example.cache;
    
    
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import redis.clients.jedis.Jedis;
    import redis.clients.jedis.JedisPool;
    
    @Configuration
    public class RedisConfig {
    
    
        @Bean(name="redisPool")
        public JedisPool jedisPool(@Value("${jedis.host}") String host,
                                   @Value("${jedis.port}") int port){
    
            return new JedisPool(host,port);
        }
    
    }
    
    • RedisClient
    package com.alan.concurrency.example.cache;
    
    
    import org.springframework.stereotype.Component;
    import redis.clients.jedis.Jedis;
    import redis.clients.jedis.JedisPool;
    
    import javax.annotation.Resource;
    
    @Component
    public class RedisClient {
    
    
        @Resource(name="redisPool")
        private JedisPool jedisPool;
    
    
        public void set(String key,String value) throws Exception{
    
            Jedis jedis = null;
    
            try {
                jedis = jedisPool.getResource();
                jedis.set(key,value);
    
            } finally {
    
                if (jedis != null){
                    jedis.close();
                }
            }
        }
    
    
        public String get(String key) throws Exception{
            Jedis jedis = null;
    
            try {
                jedis = jedisPool.getResource();
                return  jedis.get(key);
    
            } finally {
    
                if (jedis != null){
                    jedis.close();
                }
            }
    
        }
    
    }
    
    • CacheController
    package com.alan.concurrency.example.cache;
    
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.bind.annotation.ResponseBody;
    
    @Controller
    @RequestMapping("/cache")
    public class CacheController {
    
    
        @Autowired
        private RedisClient redisClient;
    
    
        @RequestMapping("/set")
        @ResponseBody
        public String set(@RequestParam("k") String k , @RequestParam("v") String v) throws  Exception{
    
            redisClient.set(k,v);
            return "SUCCESS";
    
        }
    
    
        @RequestMapping("/get")
        @ResponseBody
        public String get(@RequestParam("k") String k) throws  Exception{
             return  redisClient.get(k);
    
        }
    
    }
    

    高并发场景下缓存常见问题

    图片.png 图片.png
    图片.png 图片.png

    相关文章

      网友评论

          本文标题:高并发扩容之缓存

          本文链接:https://www.haomeiwen.com/subject/rzxdxqtx.html