net.spy.memcached.DefaultHashAlgorithm Java Examples

The following examples show how to use net.spy.memcached.DefaultHashAlgorithm. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MemcacheClientFactoryImpl.java    From simple-spring-memcached with MIT License 6 votes vote down vote up
@Override
public CacheClient create(final List<InetSocketAddress> addrs, final CacheConfiguration conf) throws IOException {
    // currently its works because this factory creates clients with the same connection settings, only memcached
    // addresses can be changed
    if (connectionFactory == null) {
        ConnectionFactoryBuilder builder = new ConnectionFactoryBuilder();

        if (conf.isConsistentHashing()) {
            builder.setHashAlg(DefaultHashAlgorithm.KETAMA_HASH);
            builder.setLocatorType(Locator.CONSISTENT);
        }

        builder.setProtocol(conf.isUseBinaryProtocol() ? Protocol.BINARY : Protocol.TEXT);
        if (conf.getOperationTimeout() != null) {
            builder.setOpTimeout(conf.getOperationTimeout());
        }

        if (conf instanceof SpymemcachedConfiguration) {
            setProviderSpecificSettings(builder, (SpymemcachedConfiguration) conf);
        }

        connectionFactory = builder.build();
    }

    return new MemcacheClientWrapper(new MemcachedClient(connectionFactory, addrs));
}
 
Example #2
Source File: EntityTestUtils.java    From hibernate4-memcached with Apache License 2.0 6 votes vote down vote up
public static void init() {
    Map<String, Object> props = new HashMap<String, Object>();
    props.put(AvailableSettings.USE_SECOND_LEVEL_CACHE, true);
    props.put(AvailableSettings.USE_QUERY_CACHE, true);
    props.put(AvailableSettings.DEFAULT_CACHE_CONCURRENCY_STRATEGY, CacheConcurrencyStrategy.NONSTRICT_READ_WRITE);
    props.put(AvailableSettings.CACHE_REGION_FACTORY, Hibernate4MemcachedRegionFactory.class.getName());
    props.put(AvailableSettings.CACHE_REGION_PREFIX, "cachetest");
    props.put(AvailableSettings.CACHE_PROVIDER_CONFIG, "META-INF/h4m-properties.xml");
    props.put(AvailableSettings.HBM2DDL_AUTO, "create");
    props.put(AvailableSettings.USE_STRUCTURED_CACHE, "false");
    props.put(Hibernate4MemcachedRegionFactory.MEMCACHED_ADAPTER_CLASS_PROPERTY_KEY,
            SpyMemcachedAdapter.class.getName());
    props.put(SpyMemcachedAdapter.HOST_PROPERTY_KEY, "localhost:11211");
    props.put(SpyMemcachedAdapter.HASH_ALGORITHM_PROPERTY_KEY, DefaultHashAlgorithm.KETAMA_HASH.name());
    props.put(SpyMemcachedAdapter.OPERATION_TIMEOUT_MILLIS_PROPERTY_KEY, "5000");
    props.put(SpyMemcachedAdapter.TRANSCODER_PROPERTY_KEY, KryoTranscoder.class.getName());
    props.put(SpyMemcachedAdapter.CACHE_KEY_PREFIX_PROPERTY_KEY, "h4m");
    props.put(KryoTranscoder.COMPRESSION_THREASHOLD_PROPERTY_KEY, "20000");

    emf = Persistence.createEntityManagerFactory("cachetest", props);
}
 
Example #3
Source File: MemcachedCache.java    From kylin-on-parquet-v2 with Apache License 2.0 5 votes vote down vote up
public static MemcachedCache create(final MemcachedCacheConfig config, String memcachedPrefix, int timeToLive) {
    try {
        SerializingTranscoder transcoder = new SerializingTranscoder(config.getMaxObjectSize());
        // always no compression inside, we compress/decompress outside
        transcoder.setCompressionThreshold(Integer.MAX_VALUE);

        OperationQueueFactory opQueueFactory;
        int maxQueueSize = config.getMaxOperationQueueSize();
        if (maxQueueSize > 0) {
            opQueueFactory = new ArrayOperationQueueFactory(maxQueueSize);
        } else {
            opQueueFactory = new LinkedOperationQueueFactory();
        }
        String hostsStr = config.getHosts();
        ConnectionFactory connectionFactory = new MemcachedConnectionFactoryBuilder()
                .setProtocol(ConnectionFactoryBuilder.Protocol.BINARY)
                .setHashAlg(DefaultHashAlgorithm.FNV1A_64_HASH)
                .setLocatorType(ConnectionFactoryBuilder.Locator.CONSISTENT).setDaemon(true)
                .setFailureMode(FailureMode.Redistribute).setTranscoder(transcoder).setShouldOptimize(true)
                .setOpQueueMaxBlockTime(config.getTimeout()).setOpTimeout(config.getTimeout())
                .setReadBufferSize(config.getReadBufferSize()).setOpQueueFactory(opQueueFactory).build();
        return new MemcachedCache(new MemcachedClient(new MemcachedConnectionFactory(connectionFactory),
                AddrUtil.getAddresses(hostsStr)), config, memcachedPrefix, timeToLive);
    } catch (IOException e) {
        logger.error("Unable to create MemcachedCache instance.", e);
        throw Throwables.propagate(e);
    }
}
 
Example #4
Source File: MemcachedCache.java    From kylin with Apache License 2.0 5 votes vote down vote up
public static MemcachedCache create(final MemcachedCacheConfig config, String memcachedPrefix, int timeToLive) {
    try {
        SerializingTranscoder transcoder = new SerializingTranscoder(config.getMaxObjectSize());
        // always no compression inside, we compress/decompress outside
        transcoder.setCompressionThreshold(Integer.MAX_VALUE);

        OperationQueueFactory opQueueFactory;
        int maxQueueSize = config.getMaxOperationQueueSize();
        if (maxQueueSize > 0) {
            opQueueFactory = new ArrayOperationQueueFactory(maxQueueSize);
        } else {
            opQueueFactory = new LinkedOperationQueueFactory();
        }
        String hostsStr = config.getHosts();
        ConnectionFactory connectionFactory = new MemcachedConnectionFactoryBuilder()
                .setProtocol(ConnectionFactoryBuilder.Protocol.BINARY)
                .setHashAlg(DefaultHashAlgorithm.FNV1A_64_HASH)
                .setLocatorType(ConnectionFactoryBuilder.Locator.CONSISTENT).setDaemon(true)
                .setFailureMode(FailureMode.Redistribute).setTranscoder(transcoder).setShouldOptimize(true)
                .setOpQueueMaxBlockTime(config.getTimeout()).setOpTimeout(config.getTimeout())
                .setReadBufferSize(config.getReadBufferSize()).setOpQueueFactory(opQueueFactory).build();
        return new MemcachedCache(new MemcachedClient(new MemcachedConnectionFactory(connectionFactory),
                getResolvedAddrList(hostsStr)), config, memcachedPrefix, timeToLive);
    } catch (IOException e) {
        logger.error("Unable to create MemcachedCache instance.", e);
        throw Throwables.propagate(e);
    }
}
 
Example #5
Source File: SpyMemcacheClientFactoryIT.java    From hibernate-l2-memcached with Apache License 2.0 5 votes vote down vote up
@Test
public void testAllPropertiesSet() throws Exception {

    properties.setProperty("hibernate.memcached.servers", "localhost:11211 localhost:11212");
    properties.setProperty("hibernate.memcached.hashAlgorithm", DefaultHashAlgorithm.CRC_HASH.name());
    properties.setProperty("hibernate.memcached.operationQueueLength", "8192");
    properties.setProperty("hibernate.memcached.readBufferLength", "8192");
    properties.setProperty("hibernate.memcached.operationTimeout", "5000");
    properties.setProperty("hibernate.memcached.daemonMode", "true");

    client = factory.createMemcacheClient();
    Assert.assertNotNull(client);
}
 
Example #6
Source File: MemcacheClientFactoryImpl.java    From simple-spring-memcached with MIT License 5 votes vote down vote up
@Override
public CacheClient create(final List<InetSocketAddress> addrs, final CacheConfiguration conf) throws IOException {
    // currently its works because this factory creates clients with the same connection settings, only memcached
    // addresses can be changed
    if (connectionFactory == null) {
        ElastiCacheConfiguration elasticacheConf = null;
        if (conf instanceof ElastiCacheConfiguration) {
            elasticacheConf = (ElastiCacheConfiguration) conf;
        }
        
        if (elasticacheConf != null && Boolean.TRUE.equals(elasticacheConf.getUseAutoDiscovery())) {
            // there is no way to use custom client settings and auto discovery together
            LOGGER.info("All cache settings will be ignored because useAutoDiscovery is true");
            return new MemcacheClientWrapper(new MemcachedClient(addrs));
        }
        
        
        ConnectionFactoryBuilder builder = new ConnectionFactoryBuilder();

        if (conf.isConsistentHashing()) {
            builder.setHashAlg(DefaultHashAlgorithm.KETAMA_HASH);
            builder.setLocatorType(Locator.CONSISTENT);
        }

        builder.setProtocol(conf.isUseBinaryProtocol() ? Protocol.BINARY : Protocol.TEXT);
        if (conf.getOperationTimeout() != null) {
            builder.setOpTimeout(conf.getOperationTimeout());
        }

        if (elasticacheConf != null) {
            setProviderSpecificSettings(builder, elasticacheConf);
        }

        connectionFactory = builder.build();
    }

    return new MemcacheClientWrapper(new MemcachedClient(connectionFactory, addrs));
}
 
Example #7
Source File: EVCacheNodeLocator.java    From EVCache with Apache License 2.0 5 votes vote down vote up
/**
 * Setup the KetamaNodeLocator with the list of nodes it should use.
 *
 * @param nodes
 *            a List of MemcachedNodes for this KetamaNodeLocator to use in
 *            its continuum
 */
protected final void setKetamaNodes(List<MemcachedNode> nodes) {
    TreeMap<Long, MemcachedNode> newNodeMap = new TreeMap<Long, MemcachedNode>();
    final int numReps = config.getNodeRepetitions();
    for (MemcachedNode node : nodes) {
        // Ketama does some special work with md5 where it reuses chunks.
        if (hashingAlgorithm == DefaultHashAlgorithm.KETAMA_HASH) {
            for (int i = 0; i < numReps / 4; i++) {
                final String hashString = config.getKeyForNode(node, i);
                byte[] digest = DefaultHashAlgorithm.computeMd5(hashString);
                if (log.isDebugEnabled()) log.debug("digest : " + digest);
                for (int h = 0; h < 4; h++) {
                    long k = ((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);
                    newNodeMap.put(Long.valueOf(k), node);
                    if (log.isDebugEnabled()) log.debug("Key : " + hashString + " ; hash : " + k + "; node " + node );
                }
            }
        } else {
            for (int i = 0; i < numReps; i++) {
                final Long hashL = Long.valueOf(hashingAlgorithm.hash(config.getKeyForNode(node, i)));
                newNodeMap.put(hashL, node);
            }
        }
    }
    if (log.isDebugEnabled()) log.debug("NewNodeMapSize : " + newNodeMap.size() + "; MapSize : " + (numReps * nodes.size()));
    if (log.isTraceEnabled()) {
        for(Long key : newNodeMap.keySet()) {
            log.trace("Hash : " + key + "; Node : " + newNodeMap.get(key));
        }
    }
    ketamaNodes = newNodeMap;
}
 
Example #8
Source File: BaseConnectionFactory.java    From EVCache with Apache License 2.0 5 votes vote down vote up
BaseConnectionFactory(EVCacheClient client, int len, Property<Integer> _operationTimeout, long opMaxBlockTime) {
    super(len, BinaryConnectionFactory.DEFAULT_READ_BUFFER_SIZE, DefaultHashAlgorithm.KETAMA_HASH);
    this.opMaxBlockTime = opMaxBlockTime;
    this.operationTimeout = _operationTimeout;
    this.client = client;
    this.startTime = System.currentTimeMillis();

    this.appName = client.getAppName();
    this.failureMode = client.getPool().getEVCacheClientPoolManager().getEVCacheConfig().getPropertyRepository().get(this.client.getServerGroupName() + ".failure.mode", String.class).orElseGet(appName + ".failure.mode").orElse("Retry");
    this.name = appName + "-" + client.getServerGroupName() + "-" + client.getId();
}
 
Example #9
Source File: BaseAsciiConnectionFactory.java    From EVCache with Apache License 2.0 5 votes vote down vote up
BaseAsciiConnectionFactory(EVCacheClient client, int len, Property<Integer> _operationTimeout, long opMaxBlockTime) {
    super(len, DefaultConnectionFactory.DEFAULT_READ_BUFFER_SIZE, DefaultHashAlgorithm.KETAMA_HASH);
    this.opMaxBlockTime = opMaxBlockTime;
    this.operationTimeout = _operationTimeout;
    this.client = client;
    this.startTime = System.currentTimeMillis();

    this.appName = client.getAppName();
    this.failureMode = client.getPool().getEVCacheClientPoolManager().getEVCacheConfig().getPropertyRepository().get(this.client.getServerGroupName() + ".failure.mode", String.class).orElseGet(appName + ".failure.mode").orElse("Retry");
    this.name = appName + "-" + client.getServerGroupName() + "-" + client.getId();
}
 
Example #10
Source File: BaseConnectionFactory.java    From EVCache with Apache License 2.0 4 votes vote down vote up
public NodeLocator createLocator(List<MemcachedNode> list) {
    this.locator = new EVCacheNodeLocator(client, list,
            DefaultHashAlgorithm.KETAMA_HASH, new EVCacheKetamaNodeLocatorConfiguration(client));
    return locator;
}
 
Example #11
Source File: BaseAsciiConnectionFactory.java    From EVCache with Apache License 2.0 4 votes vote down vote up
public NodeLocator createLocator(List<MemcachedNode> list) {
    this.locator = new EVCacheNodeLocator(client, list, DefaultHashAlgorithm.KETAMA_HASH, new EVCacheKetamaNodeLocatorConfiguration(client));
    return locator;
}
 
Example #12
Source File: DIConnectionFactory.java    From EVCache with Apache License 2.0 4 votes vote down vote up
@Override
public NodeLocator createLocator(List<MemcachedNode> list) {
    this.locator = new EVCacheNodeLocator(client, list,  DefaultHashAlgorithm.KETAMA_HASH, new DIEVCacheKetamaNodeLocatorConfiguration(client, eurekaClient));
    return locator;
}
 
Example #13
Source File: DIAsciiConnectionFactory.java    From EVCache with Apache License 2.0 4 votes vote down vote up
@Override
public NodeLocator createLocator(List<MemcachedNode> list) {
    this.locator = new EVCacheNodeLocator(client, list,  DefaultHashAlgorithm.KETAMA_HASH, new DIEVCacheKetamaNodeLocatorConfiguration(client, eurekaClient));
    return locator;
}