ddth-cache-adapter-core/src/main/java/com/github/ddth/cacheadapter/cacheimpl/redis/ClusteredRedisCache.java
@Override
    public void set(String key, Object entry, long expireAfterWrite, long expireAfterAccess) {
        final String KEY = calcCacheKey(key);
        try {
            JedisCluster jedisCluster = getJedis();
            /*
             * Returns -1 if no expiry, -2 if key does not exist.
             */
            final long currentTTL = jedisCluster.ttl(KEY);
            long ttl = TTL_NO_CHANGE;
            if (!(entry instanceof CacheEntry)) {
                CacheEntry ce = new CacheEntry(key, entry, expireAfterWrite, expireAfterAccess);
                entry = ce;
                ttl = expireAfterAccess > 0 ? expireAfterAccess
                        : (expireAfterWrite > 0 ? expireAfterWrite
                                : (timeToLiveSeconds > 0 ? timeToLiveSeconds : 0));
            } else {
                CacheEntry ce = (CacheEntry) entry;
                ttl = ce.getExpireAfterAccess();
            }
            byte[] data = serializeCacheEntry((CacheEntry) entry);

            // TTL Rules:
            // 1. New item: TTL is calculated as formula(s) above.
            // 2. Existing item:
            // 2.1. If [keyMode=HASH]: extends the current TTL,
            // 2.2. Otherwise, extends the current TTL only when
            // expireAfterAccess >
            // 0
            if (currentTTL >= -1) {
                // existing item
                if (keyMode == KeyMode.HASH)
                    // rule 2.1
                    ttl = currentTTL > 0 ? currentTTL : TTL_NO_CHANGE;
                else
                    // rule 2.2
                    ttl = expireAfterAccess > 0 ? expireAfterAccess : TTL_NO_CHANGE;
            }

            if (keyMode == KeyMode.HASH) {
                jedisCluster.hset(SafeEncoder.encode(getName()), SafeEncoder.encode(KEY), data);
                if (ttl > 0) {
                    jedisCluster.expire(getName(), (int) ttl);
                } else if (ttl == TTL_FOREVER && currentTTL >= -1) {
                    jedisCluster.persist(getName());
                }
            } else {
                if (ttl > 0) {
                    jedisCluster.setex(SafeEncoder.encode(KEY), (int) ttl, data);
                } else {
                    jedisCluster.set(SafeEncoder.encode(KEY), data);
                    if (ttl == TTL_FOREVER) {
                        jedisCluster.persist(KEY);
                    } else if (currentTTL > 0) {
                        jedisCluster.expire(KEY, (int) currentTTL);
                    }
                }
            }
        } catch (Exception e) {
            throw e instanceof CacheException ? (CacheException) e : new CacheException(e);
        }
    }
@Override
    public void set(String key, Object entry, long expireAfterWrite, long expireAfterAccess) {
        final String KEY = calcCacheKey(key);
        try {
            JedisCluster jedisCluster = getJedis();
            /*
             * Returns -1 if no expiry, -2 if key does not exist.
             */
            final long currentTTL = jedisCluster.ttl(KEY);
            long ttl = TTL_NO_CHANGE;
            if (!(entry instanceof CacheEntry)) {
                CacheEntry ce = new CacheEntry(key, entry, expireAfterWrite, expireAfterAccess);
                entry = ce;
                ttl = expireAfterAccess > 0 ? expireAfterAccess
                        : (expireAfterWrite > 0 ? expireAfterWrite
                                : (timeToLiveSeconds > 0 ? timeToLiveSeconds : 0));
            } else {
                CacheEntry ce = (CacheEntry) entry;
                ttl = ce.getExpireAfterAccess();
            }
            byte[] data = serializeCacheEntry((CacheEntry) entry);

            // TTL Rules:
            // 1. New item: TTL is calculated as formula(s) above.
            // 2. Existing item:
            // 2.1. If [keyMode=HASH]: extends the current TTL,
            // 2.2. Otherwise, extends the current TTL only when
            // expireAfterAccess >
            // 0
            if (currentTTL >= -1) {
                // existing item
                if (keyMode == KeyMode.HASH)
                    // rule 2.1
                    ttl = currentTTL > 0 ? currentTTL : TTL_NO_CHANGE;
                else
                    // rule 2.2
                    ttl = expireAfterAccess > 0 ? expireAfterAccess : TTL_NO_CHANGE;
            }

            if (keyMode == KeyMode.HASH) {
                jedisCluster.hset(SafeEncoder.encode(getName()), SafeEncoder.encode(KEY), data);
                if (ttl > 0) {
                    jedisCluster.expire(getName(), (int) ttl);
                } else if (ttl == TTL_FOREVER && currentTTL >= -1) {
                    jedisCluster.persist(getName());
                }
            } else {
                if (ttl > 0) {
                    jedisCluster.setex(SafeEncoder.encode(KEY), (int) ttl, data);
                } else {
                    jedisCluster.set(SafeEncoder.encode(KEY), data);
                    if (ttl == TTL_FOREVER) {
                        jedisCluster.persist(KEY);
                    } else if (currentTTL > 0) {
                        jedisCluster.expire(KEY, (int) currentTTL);
                    }
                }
            }
        } catch (Exception e) {
            throw e instanceof CacheException ? (CacheException) e : new CacheException(e);
        }
    }
@ Override public void set ( String key , Object entry , long expireAfterWrite , long expireAfterAccess ) { final String KEY = calcCacheKey ( key ) ; try { JedisCluster jedisCluster = getJedis ( ) ; /*
             * Returns -1 if no expiry, -2 if key does not exist.
             */ final long currentTTL = jedisCluster . ttl ( KEY ) ; long ttl = TTL_NO_CHANGE ; if ( ! ( entry instanceof CacheEntry ) ) { CacheEntry ce = new CacheEntry ( key , entry , expireAfterWrite , expireAfterAccess ) ; entry = ce ; ttl = expireAfterAccess > 0 ? expireAfterAccess : ( expireAfterWrite > 0 ? expireAfterWrite : ( timeToLiveSeconds > 0 ? timeToLiveSeconds : 0 ) ) ; } else { CacheEntry ce = ( CacheEntry ) entry ; ttl = ce . getExpireAfterAccess ( ) ; } byte [ ] data = serializeCacheEntry ( ( CacheEntry ) entry ) ; // TTL Rules: // 1. New item: TTL is calculated as formula(s) above. // 2. Existing item: // 2.1. If [keyMode=HASH]: extends the current TTL, // 2.2. Otherwise, extends the current TTL only when // expireAfterAccess > // 0 if ( currentTTL >= - 1 ) { // existing item if ( keyMode == KeyMode . HASH ) // rule 2.1 ttl = currentTTL > 0 ? currentTTL : TTL_NO_CHANGE ; else // rule 2.2 ttl = expireAfterAccess > 0 ? expireAfterAccess : TTL_NO_CHANGE ; } if ( keyMode == KeyMode . HASH ) { jedisCluster . hset ( SafeEncoder . encode ( getName ( ) ) , SafeEncoder . encode ( KEY ) , data ) ; if ( ttl > 0 ) { jedisCluster . expire ( getName ( ) , ( int ) ttl ) ; } else if ( ttl == TTL_FOREVER && currentTTL >= - 1 ) { jedisCluster . persist ( getName ( ) ) ; } } else { if ( ttl > 0 ) { jedisCluster . setex ( SafeEncoder . encode ( KEY ) , ( int ) ttl , data ) ; } else { jedisCluster . set ( SafeEncoder . encode ( KEY ) , data ) ; if ( ttl == TTL_FOREVER ) { jedisCluster . persist ( KEY ) ; } else if ( currentTTL > 0 ) { jedisCluster . expire ( KEY , ( int ) currentTTL ) ; } } } } catch ( Exception e ) { throw e instanceof CacheException ? ( CacheException ) e : new CacheException ( e ) ; } }
{@inheritDoc}
{