zoukankan      html  css  js  c++  java
  • 执行器模式设计和使用

    1.定义: 
    @Autowired
        private JedisPool jedisPool;
    
     
        public static interface Execution {
            public <T> T process(Jedis jedis);
        }
    
        /**
         * 执行请求,开始前获取资源,结束后返回资源
         *
         * @param execution
         * @return
         */
        public <T> T execute(Execution execution) {
            Jedis jedis = null;
            try {
                jedis = jedisPool.getResource();
                T result = execution.process(jedis);
                return result;
            } catch (JedisConnectionException e) {
                throw e;
            } finally {
                if (null != jedis) {
                    jedis.close();
                }
            }
        }
    
    2.使用:
    
     @Autowired
        private JedisExecutionService jedisExecutionService;
    
        public void set(final String key, final Object value, final int seconds) {
    
            if (null == key) {
                throw new IllegalArgumentException("Cached key cannot be null.");
            }
            if (null == value) {
                throw new IllegalArgumentException("Cached value cannot be null.");
            }
    
            jedisExecutionService.execute(new Execution() {
    
                @Override
                public Object process(Jedis jedis) {
                    byte[] byteArray = serialize(value);
                    jedis.set(key.getBytes(), byteArray);
                    if (seconds > 0) {
                        jedis.expire(key.getBytes(), seconds);
                    }
                    return null;
                }
    
            });
        }
    
    
     public void expire(final String key, final int seconds) {
            if (null == key) {
                throw new IllegalArgumentException("Cached key cannot be null.");
            }
    
            jedisExecutionService.execute(new Execution() {
    
                @Override
                public Object process(Jedis jedis) {
                    jedis.expire(key.getBytes(), seconds);
                    return null;
                }
    
            });
        }
    
    
      public Object get(final String key) {
    
            if (null == key) {
                throw new IllegalArgumentException("Cached key cannot be null.");
            }
    
            return jedisExecutionService.execute(new Execution() {
    
                @Override
                public Object process(Jedis jedis) {
                    byte[] bs = jedis.get(key.getBytes());
                    if (null == bs) {
                        return null;
                    } else {
                        return deserialize(bs);
                    }
                }
    
            });
    
        }
    

      

  • 相关阅读:
    Nginx、PCRE和中文URL(UTF8编码)rewrite路径重写匹配问题
    Nginx 使用中文URL,中文目录路径
    再谈Nginx Rewrite, 中文URL和其它
    事务管理
    commons-dbcp连接池的使用
    JDBC操作简单实用了IOUtils
    JDBC进行处理大文件和批处理
    mysql日期函数(转)
    mysql约束(自己原先总结的有点不准)
    mysql笔记(前面自己写的不标准有些地方)
  • 原文地址:https://www.cnblogs.com/yuhuameng/p/7520972.html
Copyright © 2011-2022 走看看