use of org.redisson.client.protocol.convertor.NumberConvertor in project redisson by redisson.
the class RedissonMapCache method addAndGetOperationAsync.
@Override
protected RFuture<V> addAndGetOperationAsync(K key, Number value) {
ByteBuf keyState = encodeMapKey(key);
String name = getRawName(key);
return commandExecutor.evalWriteAsync(name, StringCodec.INSTANCE, new RedisCommand<Object>("EVAL", new NumberConvertor(value.getClass())), "local value = redis.call('hget', KEYS[1], ARGV[2]); " + "local expireDate = 92233720368547758; " + "local t = 0; " + "local val = 0; " + "if value ~= false then " + "t, val = struct.unpack('dLc0', value); " + "local expireDateScore = redis.call('zscore', KEYS[2], ARGV[2]); " + "if expireDateScore ~= false then " + "expireDate = tonumber(expireDateScore) " + "end; " + "if t ~= 0 then " + "local expireIdle = redis.call('zscore', KEYS[3], ARGV[2]); " + "if expireIdle ~= false then " + "if tonumber(expireIdle) > tonumber(ARGV[1]) then " + "redis.call('zadd', KEYS[3], t + tonumber(ARGV[1]), ARGV[2]); " + "end; " + "expireDate = math.min(expireDate, tonumber(expireIdle)) " + "end; " + "end; " + "end; " + "local newValue = tonumber(ARGV[3]); " + "if value ~= false and expireDate > tonumber(ARGV[1]) then " + "newValue = tonumber(val) + newValue; " + "local msg = struct.pack('Lc0Lc0Lc0', string.len(ARGV[2]), ARGV[2], string.len(newValue), newValue, string.len(val), val); " + "redis.call('publish', KEYS[5], msg); " + "else " + "local msg = struct.pack('Lc0Lc0', string.len(ARGV[2]), ARGV[2], string.len(ARGV[3]), ARGV[3]); " + "redis.call('publish', KEYS[4], msg); " + "end; " + "local newValuePack = struct.pack('dLc0', t, string.len(newValue), newValue); " + "redis.call('hset', KEYS[1], ARGV[2], newValuePack); " + // last access time
"local maxSize = tonumber(redis.call('hget', KEYS[8], 'max-size')); " + "if maxSize ~= nil and maxSize ~= 0 then " + " local currentTime = tonumber(ARGV[1]); " + " local lastAccessTimeSetName = KEYS[6]; " + "local mode = redis.call('hget', KEYS[8], 'mode'); " + "if mode == false or mode == 'LRU' then " + "redis.call('zadd', lastAccessTimeSetName, currentTime, ARGV[2]); " + "end; " + " local cacheSize = tonumber(redis.call('hlen', KEYS[1])); " + " if cacheSize > maxSize then " + " local lruItems = redis.call('zrange', lastAccessTimeSetName, 0, cacheSize - maxSize - 1); " + " for index, lruItem in ipairs(lruItems) do " + " if lruItem and lruItem ~= ARGV[2] then " + " local lruItemValue = redis.call('hget', KEYS[1], lruItem); " + " redis.call('hdel', KEYS[1], lruItem); " + " redis.call('zrem', KEYS[2], lruItem); " + " redis.call('zrem', KEYS[3], lruItem); " + " redis.call('zrem', lastAccessTimeSetName, lruItem); " + " if lruItemValue ~= false then " + " local removedChannelName = KEYS[7]; " + "local ttl, obj = struct.unpack('dLc0', lruItemValue);" + " local msg = struct.pack('Lc0Lc0', string.len(lruItem), lruItem, string.len(obj), obj);" + " redis.call('publish', removedChannelName, msg); " + "end; " + " end; " + " end; " + " end; " + "if mode == 'LFU' then " + "redis.call('zincrby', lastAccessTimeSetName, 1, ARGV[2]); " + "end; " + "end; " + "return tostring(newValue); ", Arrays.<Object>asList(name, getTimeoutSetName(name), getIdleSetName(name), getCreatedChannelName(name), getUpdatedChannelName(name), getLastAccessTimeSetName(name), getRemovedChannelName(name), getOptionsName(name)), System.currentTimeMillis(), keyState, new BigDecimal(value.toString()).toPlainString());
}
use of org.redisson.client.protocol.convertor.NumberConvertor in project redisson by redisson.
the class RedissonReactiveNumberCommands method decrBy.
@Override
public <T extends Number> Flux<NumericResponse<DecrByCommand<T>, T>> decrBy(Publisher<DecrByCommand<T>> commands) {
return execute(commands, key -> {
Assert.notNull(key.getKey(), "Key must not be null!");
Assert.notNull(key.getValue(), "Value must not be null!");
byte[] keyBuf = toByteArray(key.getKey());
Mono<T> m = write(keyBuf, StringCodec.INSTANCE, new RedisCommand<Object>("INCRBYFLOAT", new NumberConvertor(key.getValue().getClass())), keyBuf, "-" + new BigDecimal(key.getValue().toString()).toPlainString());
return m.map(v -> new NumericResponse<>(key, v));
});
}
use of org.redisson.client.protocol.convertor.NumberConvertor in project redisson by redisson.
the class RedissonMap method addAndGetOperationAsync.
protected RFuture<V> addAndGetOperationAsync(K key, Number value) {
ByteBuf keyState = encodeMapKey(key);
String name = getRawName(key);
RFuture<V> future = commandExecutor.writeAsync(name, StringCodec.INSTANCE, new RedisCommand<>("HINCRBYFLOAT", new NumberConvertor(value.getClass())), name, keyState, new BigDecimal(value.toString()).toPlainString());
return future;
}
use of org.redisson.client.protocol.convertor.NumberConvertor in project redisson by redisson.
the class RedissonLocalCachedMap method addAndGetOperationAsync.
@Override
protected RFuture<V> addAndGetOperationAsync(K key, Number value) {
ByteBuf keyState = encodeMapKey(key);
CacheKey cacheKey = localCacheView.toCacheKey(keyState);
ByteBuf msg = encode(new LocalCachedMapInvalidate(instanceId, cacheKey.getKeyHash()));
byte[] entryId = generateLogEntryId(cacheKey.getKeyHash());
RFuture<V> future = commandExecutor.evalWriteAsync(getRawName(), StringCodec.INSTANCE, new RedisCommand<Object>("EVAL", new NumberConvertor(value.getClass())), "local result = redis.call('HINCRBYFLOAT', KEYS[1], ARGV[1], ARGV[2]); " + "if ARGV[3] == '1' then " + "redis.call('publish', KEYS[2], ARGV[4]); " + "end;" + "if ARGV[3] == '2' then " + "redis.call('zadd', KEYS[3], ARGV[5], ARGV[6]);" + "redis.call('publish', KEYS[2], ARGV[4]); " + "end;" + "return result; ", Arrays.<Object>asList(getRawName(), listener.getInvalidationTopicName(), listener.getUpdatesLogName()), keyState, new BigDecimal(value.toString()).toPlainString(), invalidateEntryOnChange, msg, System.currentTimeMillis(), entryId);
CompletionStage<V> f = future.thenApply(res -> {
if (res != null) {
CacheKey cKey = localCacheView.toCacheKey(key);
cachePut(cKey, key, res);
}
return res;
});
return new CompletableFutureWrapper<>(f);
}
use of org.redisson.client.protocol.convertor.NumberConvertor in project redisson by redisson.
the class BaseTransactionalMap method addAndGetOperationAsync.
protected RFuture<V> addAndGetOperationAsync(K key, Number value) {
RPromise<V> result = new RedissonPromise<V>();
long threadId = Thread.currentThread().getId();
executeLocked(result, key, new Runnable() {
@Override
public void run() {
HashValue keyHash = toKeyHash(key);
MapEntry entry = state.get(keyHash);
if (entry != null) {
BigDecimal currentValue = BigDecimal.ZERO;
if (entry != MapEntry.NULL) {
currentValue = (BigDecimal) entry.getValue();
}
BigDecimal res = currentValue.add(new BigDecimal(value.toString()));
operations.add(new MapAddAndGetOperation(map, key, value, transactionId, threadId));
state.put(keyHash, new MapEntry(key, res));
if (deleted != null) {
deleted = false;
}
NumberConvertor convertor = new NumberConvertor(value.getClass());
result.trySuccess((V) convertor.convert(res.toPlainString()));
return;
}
map.getAsync(key).onComplete((r, e) -> {
if (e != null) {
result.tryFailure(e);
return;
}
BigDecimal currentValue = new BigDecimal(r.toString());
BigDecimal res = currentValue.add(new BigDecimal(value.toString()));
operations.add(new MapAddAndGetOperation(map, key, value, transactionId, threadId));
state.put(keyHash, new MapEntry(key, res));
if (deleted != null) {
deleted = false;
}
NumberConvertor convertor = new NumberConvertor(value.getClass());
result.trySuccess((V) convertor.convert(res.toPlainString()));
});
}
});
return result;
}
Aggregations