throw new IllegalArgumentException("The memcached get method arguments mismatch, must only one arguments. interface: " + type.getName() + ", method: " + invocation.getMethodName() + ", url: " + url); return new RpcResult(memcachedClient.get(String.valueOf(invocation.getArguments()[0]))); } else if (set.equals(invocation.getMethodName())) { if (invocation.getArguments().length != 2) { throw new IllegalArgumentException("The memcached set method arguments mismatch, must be two arguments. interface: " + type.getName() + ", method: " + invocation.getMethodName() + ", url: " + url); memcachedClient.set(String.valueOf(invocation.getArguments()[0]), expiry, invocation.getArguments()[1]); return new RpcResult(); } else if (delete.equals(invocation.getMethodName())) { throw new IllegalArgumentException("The memcached delete method arguments mismatch, must only one arguments. interface: " + type.getName() + ", method: " + invocation.getMethodName() + ", url: " + url); memcachedClient.delete(String.valueOf(invocation.getArguments()[0])); return new RpcResult(); } else {
@Override public void destroy() { super.destroy(); try { memcachedClient.shutdown(); } catch (Throwable e) { logger.warn(e.getMessage(), e); } } };
public Counter(MemcachedClient memcachedClient, String key, long initialValue) { super(); this.memcachedClient = memcachedClient; this.key = key; this.initialValue = initialValue; try { this.memcachedClient.add(key, 0, String.valueOf(this.initialValue)); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } catch (Exception e) { throw new IllegalStateException("Initialize counter failed", e); } }
@Override public SessionData get(String sessionId) { try { return this.sessions.get(sessionId); } catch (TimeoutException | InterruptedException | MemcachedException ex) { log.error("An error occurred when get SessionData.", ex); return null; } }
/** * Set counter's value to expected. * * @param value * @throws MemcachedException * @throws InterruptedException * @throws TimeoutException */ public void set(long value) throws MemcachedException, InterruptedException, TimeoutException { this.memcachedClient.set(this.key, 0, String.valueOf(value)); }
@Override public void delete(String sessionId) { try { this.sessions.delete(sessionId); } catch (TimeoutException | InterruptedException | MemcachedException ex) { log.error("An error occurred when deleted SessionData.", ex); } }
@Override public byte[] getValue(byte[] key) { if(client.isShutdown()){ buildClient(); } try { byte[] bytes = client. get(new String(key,"UTF-8")); return bytes; } catch (TimeoutException e) { logger.error("Failed to get cache value from memcached", e); } catch (InterruptedException e) { logger.error("Failed to get cache value from memcached", e); } catch (MemcachedException e) { logger.error("Failed to get cache value from memcached", e); } catch (UnsupportedEncodingException e) { logger.error("Failed to get cache value from memcached", e); } return null; } }
@Override public void shutdown() { if (memcached == null) { throw new IllegalStateException("Memcached client instance has not been initialized yet."); } try { if (!memcached.isShutdown()) { memcached.shutdown(); } } catch (IOException e) { String message = "Failed to shutdown memcached client because " + e.getMessage(); log.info(message); if (log.isDebugEnabled()) { log.debug(message, e); } } }
@Override public void cacheValue(byte[] key, byte[] bytes, int expireSecond) { if(client.isShutdown()){ buildClient(); } try { client.add(new String(key,"UTF-8"),expireSecond,bytes); } catch (TimeoutException e) { logger.error("Failed to add cache value by memcached",e); } catch (InterruptedException e) { logger.error("Failed to add cache value by memcached", e); } catch (MemcachedException e) { logger.error("Failed to add cache value by memcached",e); } catch (UnsupportedEncodingException e) { logger.error("Failed to add cache value by memcached", e); } }
public boolean isAlive() { return this._client != null && !this._client.isShutdown(); }
/** * Auto reconect to memcached server * * @param session */ protected void reconnect(MemcachedTCPSession session) { if (!this.client.isShutdown()) { // Prevent reconnecting repeatedly synchronized (session) { if (!session.isAllowReconnect()) { return; } session.setAllowReconnect(false); } MemcachedSession memcachedTCPSession = session; InetSocketAddressWrapper inetSocketAddressWrapper = memcachedTCPSession.getInetSocketAddressWrapper(); this.client.getConnector().addToWatingQueue( new ReconnectRequest(inetSocketAddressWrapper, 0, this.client.getHealSessionInterval())); } }
/** * Get current value * * @return * @throws MemcachedException * @throws InterruptedException * @throws TimeoutException */ public long get() throws MemcachedException, InterruptedException, TimeoutException { Object result = this.memcachedClient.get(this.key); if (result == null) { throw new MemcachedClientException("key is not existed."); } else { if (result instanceof Long) return (Long) result; else return Long.valueOf(((String) result).trim()); } }
@Override public void save(SessionData sessionData) { try { this.sessions.set(sessionData.getId(), idleTime, sessionData); } catch (TimeoutException | InterruptedException | MemcachedException ex) { log.error("An error occurred when saved SessionData.", ex); } }
@Override public boolean delete(String id) throws Exception { _client.delete(id); return true; //delete returns false if the value didn't exist }
@Override public byte[] getValue(byte[] key) { if(client.isShutdown()){ buildClient(); } try { byte[] bytes = client. get(new String(key,"UTF-8")); return bytes; } catch (TimeoutException e) { logger.error("Failed to get cache value from memcached", e); } catch (InterruptedException e) { logger.error("Failed to get cache value from memcached", e); } catch (MemcachedException e) { logger.error("Failed to get cache value from memcached", e); } catch (UnsupportedEncodingException e) { logger.error("Failed to get cache value from memcached", e); } return null; } }
@Override public void cacheValue(byte[] key, byte[] bytes, int expireSecond) { if(client.isShutdown()){ buildClient(); } try { client.add(new String(key,"UTF-8"),expireSecond,bytes); } catch (TimeoutException e) { logger.error("Failed to add cache value by memcached",e); } catch (InterruptedException e) { logger.error("Failed to add cache value by memcached", e); } catch (MemcachedException e) { logger.error("Failed to add cache value by memcached",e); } catch (UnsupportedEncodingException e) { logger.error("Failed to add cache value by memcached", e); } }
private static void buildClient(){ if(client==null||client.isShutdown()){ synchronized (MemcachedClient.class){ if(client==null||client.isShutdown()){ try { client=clientBuilder.build(); } catch (IOException e) { throw new IllegalStateException("Failed to build memcached client",e); } } } } }
/** * Auto reconect to memcached server * * @param session */ protected void reconnect(MemcachedTCPSession session) { if (!this.client.isShutdown()) { // Prevent reconnecting repeatedly synchronized (session) { if (!session.isAllowReconnect()) { return; } session.setAllowReconnect(false); } MemcachedSession memcachedTCPSession = session; InetSocketAddressWrapper inetSocketAddressWrapper = memcachedTCPSession.getInetSocketAddressWrapper(); this.client.getConnector().addToWatingQueue( new ReconnectRequest(inetSocketAddressWrapper, 0, this.client.getHealSessionInterval())); } }