コード例 #1
0
  private RedisAtomicLong(String redisCounter, RedisConnectionFactory factory, Long initialValue) {
    RedisTemplate<String, Long> redisTemplate = new RedisTemplate<String, Long>();
    redisTemplate.setKeySerializer(new StringRedisSerializer());
    redisTemplate.setValueSerializer(new GenericToStringSerializer<Long>(Long.class));
    redisTemplate.setExposeConnection(true);
    redisTemplate.setConnectionFactory(factory);
    redisTemplate.afterPropertiesSet();

    this.key = redisCounter;
    this.generalOps = redisTemplate;
    this.operations = generalOps.opsForValue();

    if (initialValue == null) {
      if (this.operations.get(redisCounter) == null) {
        set(0);
      }
    } else {
      set(initialValue);
    }
  }
コード例 #2
0
  /**
   * Atomically sets the value to the given updated value if the current value {@code ==} the
   * expected value.
   *
   * @param expect the expected value
   * @param update the new value
   * @return true if successful. False return indicates that the actual value was not equal to the
   *     expected value.
   */
  public boolean compareAndSet(final long expect, final long update) {
    return generalOps.execute(
        new SessionCallback<Boolean>() {

          @SuppressWarnings("unchecked")
          @Override
          public Boolean execute(RedisOperations operations) {
            for (; ; ) {
              operations.watch(Collections.singleton(key));
              if (expect == get()) {
                generalOps.multi();
                set(update);
                if (operations.exec() != null) {
                  return true;
                }
              }
              {
                return false;
              }
            }
          }
        });
  }
コード例 #3
0
 @Override
 public void rename(String newKey) {
   generalOps.rename(key, newKey);
   key = newKey;
 }
コード例 #4
0
 @Override
 public Boolean persist() {
   return generalOps.persist(key);
 }
コード例 #5
0
 @Override
 public Long getExpire() {
   return generalOps.getExpire(key);
 }
コード例 #6
0
 @Override
 public Boolean expireAt(Date date) {
   return generalOps.expireAt(key, date);
 }
コード例 #7
0
 @Override
 public Boolean expire(long timeout, TimeUnit unit) {
   return generalOps.expire(key, timeout, unit);
 }
コード例 #8
0
 /**
  * Constructs a new <code>DefaultRedisMap</code> instance.
  *
  * @param key
  * @param operations
  */
 public DefaultRedisMap(String key, RedisOperations<String, ?> operations) {
   this.hashOps = operations.boundHashOps(key);
 }