本文整理汇总了Java中org.springframework.data.redis.core.script.DefaultRedisScript类的典型用法代码示例。如果您正苦于以下问题:Java DefaultRedisScript类的具体用法?Java DefaultRedisScript怎么用?Java DefaultRedisScript使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
DefaultRedisScript类属于org.springframework.data.redis.core.script包,在下文中一共展示了DefaultRedisScript类的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: releaseLock
import org.springframework.data.redis.core.script.DefaultRedisScript; //导入依赖的package包/类
@Override
public void releaseLock(final String lock, final String holder) {
Assert.notNull(lock, "Pre-condition violated: lock != null.");
Assert.notNull(holder, "Pre-condition violated: holder != null.");
// KEYS[1]: key
// ARGV[1]: value
DefaultRedisScript<Void> s = new DefaultRedisScript<>(
"local lock = redis.call('get', KEYS[1]); " +
"if (lock == ARGV[1]) then " +
// We own the lock -> We may release it by deleting the lock.
"redis.call('del', KEYS[1]); " +
"end;");
redis.execute(s, keys(key(LOCK, lock)), value(holder));
}
示例2: tryLock
import org.springframework.data.redis.core.script.DefaultRedisScript; //导入依赖的package包/类
@Override
public boolean tryLock(final String lock, final String holder, final int timeout, final TimeUnit unit) {
Assert.notNull(lock, "Pre-condition violated: lock != null.");
Assert.notNull(holder, "Pre-condition violated: holder != null.");
Assert.isTrue(timeout > 0, "Pre-condition violated: timeout > 0.");
Assert.notNull(unit, "Pre-condition violated: unit != null.");
long timeoutMillis = unit.toMillis(timeout);
Assert.isTrue(timeoutMillis >= 100, "Pre-condition violated: timeoutMillis >= 100.");
// KEYS[1]: key
// ARGV[1]: timeout
// ARGV[2]: value
DefaultRedisScript<Boolean> s = new DefaultRedisScript<>(
"local lock = redis.call('get', KEYS[1]); " +
"if (lock == ARGV[2]) then " +
// We own the lock -> Refresh expiration.
"redis.call('pexpire', KEYS[1], ARGV[1]); " +
"return true; " +
"end; " +
"if (lock) then " +
// Someone else owns the lock -> Abort.
"return false; " +
"end; " +
// No one owns the lock -> Create lock with expiration.
"redis.call('psetex', KEYS[1], ARGV[1], ARGV[2]); " +
"return true;",
Boolean.class);
return redis.execute(s, keys(key(LOCK, lock)), value(timeoutMillis), value(holder));
}
示例3: redisRequestRateLimiterScript
import org.springframework.data.redis.core.script.DefaultRedisScript; //导入依赖的package包/类
@Bean
@SuppressWarnings("unchecked")
public RedisScript redisRequestRateLimiterScript() {
DefaultRedisScript redisScript = new DefaultRedisScript<>();
redisScript.setScriptSource(new ResourceScriptSource(new ClassPathResource("META-INF/scripts/request_rate_limiter.lua")));
redisScript.setResultType(List.class);
return redisScript;
}
示例4: getRedisScript
import org.springframework.data.redis.core.script.DefaultRedisScript; //导入依赖的package包/类
private <T> RedisScript<T> getRedisScript(String script, Class<T> resultType) {
return new DefaultRedisScript<>(script, resultType);
}