Ejemplo n.º 1
0
  @Override
  public void initialize(String name, String config) {
    super.initialize(name, config);
    /*
     * The config string format: <host>:<port>:<key_prefix>
     */
    String[] part = config.split(":");
    if (part.length != 3) {
      throw new IllegalArgumentException(
          "The config string" + config + " not match the format: <host>:<port>:<key_prefix>");
    }
    try {
      String[] address = new String[] {part[0] + ":" + part[1]};
      SockIOPool pool = SockIOPool.getInstance(part[0] + ":" + part[1]);
      pool.setServers(address);
      pool.setInitConn(1);
      pool.setMinConn(1);
      pool.setMaxConn(50);
      pool.setMaxIdle(1000 * 60 * 60 * 6);
      pool.setNagle(false);
      pool.setSocketTO(300);
      pool.setMaintSleep(30);
      pool.setSocketConnectTO(0);
      pool.initialize();

      client = new MemCachedClient(part[0] + ":" + part[1]);
      prefix = part[2];
    } catch (Throwable e) {
      e.printStackTrace();
      throw new IllegalArgumentException(
          "Error while construct MemcachedClient, see stderr for stack detail. " + e.getMessage());
    }
  }
Ejemplo n.º 2
0
  @Before
  public void init() {
    String[] servers = {"192.168.20.210:11211"};
    Integer[] weights = {3};
    SockIOPool pool = SockIOPool.getInstance();
    pool.setServers(servers); // 服务器列表
    pool.setWeights(weights); // 是上面服务器的权重,必须数量一致,否则权重无效
    pool.setFailover(false); // 表示对于服务器出现问题时的自动修复,true如果连接失败会连到另外一台存在的服务器,为false时,如果连接失败会返回null

    pool.setInitConn(30); // 初始的时候连接数
    pool.setMinConn(30); // 表示最小闲置连接数
    pool.setMaxConn(50); // 最大连接数
    pool.setMaxIdle(60000); // 最大空闲时间

    pool.setMaintSleep(30000); // 表示是否需要延时结束
    pool.setNagle(false); // Tcp的规则就是在发送一个包之前,本地机器会等待远程主机对上一次发送的包的确认信息到来;
    // 这个方法就可以关闭套接字的缓存,以至这个包准备好了就发;
    //		pool.setAliveCheck(true); //表示心跳检查,确定服务器的状态
    pool.setSocketTO(3000); // 是socket连接超时时间
    pool.setSocketConnectTO(0); // 连接建立时阻塞时间

    // pool.setFailback(failback);   //  检测集群节点失效后,是否进行重试,true 重试
    // pool.setHashingAlg(alg);//设置使用的hash算法

    pool.setMaxBusyTime(30000); // 给线程池里面正在用的线程设立最大的占用时间
    //		pool.setAliveCheck(true);
    // mcc.setSanitizeKeys(sanitizeKeys)设置可否存入像url这样的特殊字符,false为可以
    // mcc.setPrimitiveAsString(primitiveAsString)
    // 原本支持阻塞获取和可靠获取,并且将在存储的数据之前加上4个字节的flag(整型),因此可以支持存储任意可序列化类型。但是有一些应用只需要存储字符串类型和原生类型,这是为了在不同语言的client 之间保持可移植(如存储json数据),那么就不希望在数据之前加上这个flag,把这个值改成true就可疑了
    pool.initialize();
    //		mcc.setCompressEnable(true);//超过指定大小的压缩
    //		mcc.setCompressThreshold(64 * 1024);  //文件大小超过了这么大就压缩

  }
Ejemplo n.º 3
0
  /* (non-Javadoc)
   * @see org.hibernate.cache.CacheProvider#start(java.util.Properties)
   */
  @SuppressWarnings({"rawtypes"})
  public void start() throws CacheException {
    String conf = "/memcached.properties";
    InputStream in = getClass().getResourceAsStream(conf);
    Properties memcached_conf = new Properties();
    try {
      memcached_conf.load(in);
    } catch (IOException e) {
      throw new CacheException("Unabled to load properties from " + conf, e);
    } finally {
      IOUtils.closeQuietly(in);
    }

    String servers = memcached_conf.getProperty(SERVERS_CONF);
    if (StringUtils.isBlank(servers)) {
      throw new CacheException("configuration 'memcached.servers' get a empty value");
    }
    SockIOPool pool = SockIOPool.getInstance();
    pool.setServers(servers.split(","));

    Properties base_conf = (Properties) memcached_conf.clone();
    base_conf.remove(SERVERS_CONF);
    Iterator keys = base_conf.keySet().iterator();
    while (keys.hasNext()) {
      String key = (String) keys.next();
      if (key.startsWith(CACHE_IDENT)) {
        _cache_properties.put(key.substring(CACHE_IDENT.length()), base_conf.getProperty(key));
        keys.remove();
      }
    }
    try {
      BeanUtils.populate(pool, base_conf);
    } catch (Exception e) {
      throw new CacheException("Unabled to set properties to SockIOPool", e);
    }

    pool.initialize();

    Logger.getLogger(MemCachedClient.class.getName()).setLevel(Logger.LEVEL_WARN);

    _CacheManager = new Hashtable<String, MemCache>();
  }