帆软使用的第三方框架。
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

189 lines
6.2 KiB

7 years ago
/**
* Copyright 2018 Nikita Koksharov
7 years ago
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.fr.third.org.redisson;
7 years ago
import java.util.Collections;
import java.util.concurrent.TimeUnit;
import com.fr.third.org.redisson.api.RBucket;
import com.fr.third.org.redisson.api.RFuture;
import com.fr.third.org.redisson.client.codec.Codec;
import com.fr.third.org.redisson.client.protocol.RedisCommands;
import com.fr.third.org.redisson.api.RBucket;
import com.fr.third.org.redisson.api.RFuture;
import com.fr.third.org.redisson.client.codec.Codec;
import com.fr.third.org.redisson.client.protocol.RedisCommands;
import com.fr.third.org.redisson.command.CommandAsyncExecutor;
7 years ago
/**
*
* @author Nikita Koksharov
*
* @param <V> value type
*/
7 years ago
public class RedissonBucket<V> extends RedissonExpirable implements RBucket<V> {
public RedissonBucket(CommandAsyncExecutor connectionManager, String name) {
7 years ago
super(connectionManager, name);
}
public RedissonBucket(Codec codec, CommandAsyncExecutor connectionManager, String name) {
7 years ago
super(codec, connectionManager, name);
}
@Override
public boolean compareAndSet(V expect, V update) {
return get(compareAndSetAsync(expect, update));
}
@Override
public RFuture<Boolean> compareAndSetAsync(V expect, V update) {
7 years ago
if (expect == null && update == null) {
return trySetAsync(null);
}
if (expect == null) {
return trySetAsync(update);
}
if (update == null) {
return commandExecutor.evalWriteAsync(getName(), codec, RedisCommands.EVAL_BOOLEAN,
7 years ago
"if redis.call('get', KEYS[1]) == ARGV[1] then "
+ "redis.call('del', KEYS[1]); "
+ "return 1 "
+ "else "
+ "return 0 end",
Collections.<Object>singletonList(getName()), encode(expect));
7 years ago
}
return commandExecutor.evalWriteAsync(getName(), codec, RedisCommands.EVAL_BOOLEAN,
7 years ago
"if redis.call('get', KEYS[1]) == ARGV[1] then "
+ "redis.call('set', KEYS[1], ARGV[2]); "
+ "return 1 "
+ "else "
+ "return 0 end",
Collections.<Object>singletonList(getName()), encode(expect), encode(update));
7 years ago
}
@Override
public V getAndSet(V newValue) {
return get(getAndSetAsync(newValue));
}
@Override
public RFuture<V> getAndSetAsync(V newValue) {
7 years ago
if (newValue == null) {
return commandExecutor.evalWriteAsync(getName(), codec, RedisCommands.EVAL_OBJECT,
"local v = redis.call('get', KEYS[1]); "
+ "redis.call('del', KEYS[1]); "
+ "return v",
Collections.<Object>singletonList(getName()));
}
return commandExecutor.writeAsync(getName(), codec, RedisCommands.GETSET, getName(), encode(newValue));
7 years ago
}
@Override
public V get() {
return get(getAsync());
}
@Override
public RFuture<V> getAsync() {
7 years ago
return commandExecutor.readAsync(getName(), codec, RedisCommands.GET, getName());
}
@Override
public V getAndDelete() {
return get(getAndDeleteAsync());
}
@Override
public RFuture<V> getAndDeleteAsync() {
return commandExecutor.evalWriteAsync(getName(), codec, RedisCommands.EVAL_OBJECT,
"local currValue = redis.call('get', KEYS[1]); "
+ "redis.call('del', KEYS[1]); "
+ "return currValue; ",
Collections.<Object>singletonList(getName()));
}
@Override
public long size() {
return get(sizeAsync());
}
@Override
public RFuture<Long> sizeAsync() {
return commandExecutor.readAsync(getName(), codec, RedisCommands.STRLEN, getName());
}
7 years ago
@Override
public void set(V value) {
get(setAsync(value));
}
@Override
public RFuture<Void> setAsync(V value) {
7 years ago
if (value == null) {
return commandExecutor.writeAsync(getName(), RedisCommands.DEL_VOID, getName());
}
return commandExecutor.writeAsync(getName(), codec, RedisCommands.SET, getName(), encode(value));
7 years ago
}
@Override
public void set(V value, long timeToLive, TimeUnit timeUnit) {
get(setAsync(value, timeToLive, timeUnit));
}
@Override
public RFuture<Void> setAsync(V value, long timeToLive, TimeUnit timeUnit) {
7 years ago
if (value == null) {
throw new IllegalArgumentException("Value can't be null");
}
return commandExecutor.writeAsync(getName(), codec, RedisCommands.PSETEX, getName(), timeUnit.toMillis(timeToLive), encode(value));
7 years ago
}
@Override
public RFuture<Boolean> trySetAsync(V value) {
7 years ago
if (value == null) {
return commandExecutor.readAsync(getName(), codec, RedisCommands.NOT_EXISTS, getName());
}
return commandExecutor.writeAsync(getName(), codec, RedisCommands.SETNX, getName(), encode(value));
7 years ago
}
@Override
public RFuture<Boolean> trySetAsync(V value, long timeToLive, TimeUnit timeUnit) {
7 years ago
if (value == null) {
throw new IllegalArgumentException("Value can't be null");
}
return commandExecutor.writeAsync(getName(), codec, RedisCommands.SETPXNX, getName(), encode(value), "PX", timeUnit.toMillis(timeToLive), "NX");
7 years ago
}
@Override
public boolean trySet(V value, long timeToLive, TimeUnit timeUnit) {
return get(trySetAsync(value, timeToLive, timeUnit));
}
@Override
public boolean trySet(V value) {
return get(trySetAsync(value));
}
}