public KetamaMemcachedSessionLocator(List<Session> list, HashAlgorithm alg) { super(); this.hashAlg = alg; this.cwNginxUpstreamConsistent = false; this.gwhalinMemcachedJavaClientCompatibiltyConsistent = false; this.buildMap(list, alg); }
public final Session getSessionByKey(final String key) { if (this.ketamaSessions == null || this.ketamaSessions.size() == 0) { return null; } long hash = this.hashAlg.hash(key); Session rv = this.getSessionByHash(hash); int tries = 0; while (!this.failureMode && (rv == null || rv.isClosed()) && tries++ < this.maxTries) { hash = this.nextHash(hash, key, tries); rv = this.getSessionByHash(hash); } return rv; }
String sockStr = this.getSockStr(session); (long) (digest[3 + h * 4] & 0xFF) << 24 | (long) (digest[2 + h * 4] & 0xFF) << 16 | (long) (digest[1 + h * 4] & 0xFF) << 8 | digest[h * 4] & 0xFF; this.getSessionList(sessionMap, k).add(session); for (int i = 0; i < numReps; i++) { long key = alg.hash(sockStr + "-" + i); this.getSessionList(sessionMap, key).add(session);
@Bean public MemcachedClient memcachedClientForCluster() { List<InetSocketAddress> addressList = new ArrayList<InetSocketAddress>(); addressList.add(new InetSocketAddress("192.168.200.201", 11211)); addressList.add(new InetSocketAddress("192.168.200.201", 11212)); // 赋予权重 int[] weights = {1, 2}; XMemcachedClientBuilder builder = new XMemcachedClientBuilder(addressList, weights); // 设置连接池大小 builder.setConnectionPoolSize(10); // 协议工厂 builder.setCommandFactory(new TextCommandFactory()); // 分布策略,一致性哈希KetamaMemcachedSessionLocator或者ArraySessionLocator(默认) builder.setSessionLocator(new KetamaMemcachedSessionLocator()); // 设置序列化器 builder.setTranscoder(new SerializingTranscoder()); MemcachedClient memcachedClient = null; try { memcachedClient = builder.build(); } catch (IOException e) { e.printStackTrace(); } return memcachedClient; }
public MemcachedClient memcachedClientForCluster() { List<InetSocketAddress> addressList = new ArrayList<>(); addressList.add(new InetSocketAddress("192.168.0.108", 11211)); addressList.add(new InetSocketAddress("192.168.0.108", 11212)); // 赋予权重 int[] weights = {1, 2}; XMemcachedClientBuilder builder = new XMemcachedClientBuilder(addressList, weights); // 设置连接池大小 builder.setConnectionPoolSize(10); // 协议工厂 builder.setCommandFactory(new TextCommandFactory()); // 分布策略,一致性哈希KetamaMemcachedSessionLocator或者ArraySessionLocator(默认) builder.setSessionLocator(new KetamaMemcachedSessionLocator()); // 设置序列化器 builder.setTranscoder(new SerializingTranscoder()); MemcachedClient memcachedClient = null; try { memcachedClient = builder.build(); } catch (IOException e) { e.printStackTrace(); } return memcachedClient; }
builder.setSessionLocator(new KetamaMemcachedSessionLocator());
public final Session getSessionByKey(final String key) { if (this.ketamaSessions == null || this.ketamaSessions.size() == 0) { return null; } long hash = this.hashAlg.hash(key); Session rv = this.getSessionByHash(hash); int tries = 0; while (!this.failureMode && (rv == null || rv.isClosed()) && tries++ < this.maxTries) { hash = this.nextHash(hash, key, tries); rv = this.getSessionByHash(hash); } return rv; }
String sockStr = this.getSockStr(session); (long) (digest[3 + h * 4] & 0xFF) << 24 | (long) (digest[2 + h * 4] & 0xFF) << 16 | (long) (digest[1 + h * 4] & 0xFF) << 8 | digest[h * 4] & 0xFF; this.getSessionList(sessionMap, k).add(session); for (int i = 0; i < numReps; i++) { long key = alg.hash(sockStr + "-" + i); this.getSessionList(sessionMap, key).add(session);
public final void updateSessions(final Collection<Session> list) { this.buildMap(list, this.hashAlg); } }
public KetamaMemcachedSessionLocator(List<Session> list, HashAlgorithm alg) { super(); this.hashAlg = alg; this.cwNginxUpstreamConsistent = false; this.gwhalinMemcachedJavaClientCompatibiltyConsistent = false; this.buildMap(list, alg); }
public final void updateSessions(final Collection<Session> list) { this.buildMap(list, this.hashAlg); } }