Ejemplo n.º 1
0
 public void setServers(String remoteServers) {
   MemcachedClientFactoryBean factory = new MemcachedClientFactoryBean();
   factory.setServers(remoteServers);
   factory.setOpTimeout(MEMCACHED_TIME_OUT.toMilliseconds());
   factory.setProtocol(ConnectionFactoryBuilder.Protocol.BINARY);
   try {
     memcachedClient = (MemcachedClient) factory.getObject();
   } catch (Exception e) {
     throw new IllegalStateException(e);
   }
 }
Ejemplo n.º 2
0
public class MemcachedCacheManager extends AbstractCacheManager implements ServiceMonitor {

  private final Logger logger = LoggerFactory.getLogger(MemcachedCacheManager.class);
  private static final TimeLength MEMCACHED_TIME_OUT = TimeLength.seconds(3);

  private final List<MemcachedCache> caches = new ArrayList<MemcachedCache>();
  private MemcachedClient memcachedClient;

  public MemcachedCacheManager(String remoteServers) {
    setServers(remoteServers);
  }

  @PreDestroy
  public void shutdown() {
    logger.info("shutdown memcached cache client");
    memcachedClient.shutdown();
  }

  @Override
  protected Collection<? extends Cache> loadCaches() {
    return caches;
  }

  public void add(String cacheName, TimeLength expirationTime) {
    AssertUtils.assertNotNull(
        memcachedClient, "memcached client must be created before adding cache");
    caches.add(new MemcachedCache(cacheName, expirationTime, memcachedClient));
  }

  public void setServers(String remoteServers) {
    MemcachedClientFactoryBean factory = new MemcachedClientFactoryBean();
    factory.setServers(remoteServers);
    factory.setOpTimeout(MEMCACHED_TIME_OUT.toMilliseconds());
    factory.setProtocol(ConnectionFactoryBuilder.Protocol.BINARY);
    try {
      memcachedClient = (MemcachedClient) factory.getObject();
    } catch (Exception e) {
      throw new IllegalStateException(e);
    }
  }

  @Override
  public ServiceStatus getServiceStatus() throws Exception {
    Collection<SocketAddress> availableServers = memcachedClient.getAvailableServers();
    return availableServers.isEmpty() ? ServiceStatus.DOWN : ServiceStatus.UP;
  }

  @Override
  public String getServiceName() {
    return "Memcached";
  }
}