class CacheFactory implements AddressChangeListener, FactoryBean<Cache>, InitializingBean, DisposableBean {

    @Override
    public void changeAddresses(final List<InetSocketAddress> addresses) {
        if (isCacheDisabled()) {
            LOGGER.warn("Cache {} is disabled", cacheName);
            return;
        }

        if (!(cache instanceof CacheImpl)) {
            LOGGER.warn("This client doesn't support changing memcached addresses on the fly");
            return;
        }

        try {
            LOGGER.info("Creating new memcached client for cache {} with new addresses: {}", cacheName, addresses);
            CacheClient memcacheClient = createClient(addresses);
            LOGGER.info("New memcached client for cache {} was created with addresses: {}", cacheName, addresses);
            ((CacheImpl)cache).changeCacheClient(memcacheClient);
        } catch (IOException e) {
            if (LOGGER.isErrorEnabled()) {
                LOGGER.error(String.format("Cannot change memcached client to new one with addresses %s", addresses), e);
            }
        }
    }

}