博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
spring cloud redis lock
阅读量:6955 次
发布时间:2019-06-27

本文共 2920 字,大约阅读时间需要 9 分钟。

hot3.png

spring cloud redis lock 博客分类: java spring 缓存  

/*

 * Copyright 2015 the original author or authors.
 *
 * 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 org.springframework.cloud.cluster.redis.lock;
import java.util.concurrent.locks.Lock;
import org.springframework.cloud.cluster.lock.DistributedLock;
import org.springframework.cloud.cluster.lock.LockService;
import org.springframework.cloud.cluster.lock.support.DelegatingDistributedLock;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.integration.redis.util.RedisLockRegistry;
/**
 * {
LockService} implementation based on Redis.
 *
 * <p>This implementation delegates to {
RedisLockRegistry} from Spring
 * Integration.
 *
 * Janne Valkealahti
 *
 */
public class RedisLockService implements LockService {
   
    public static final String DEFAULT_REGISTRY_KEY = "spring-cloud";
   
    private final RedisLockRegistry redisLockRegistry;
    /**
     * Instantiates a new redis lock service.
     *
     * connectionFactory the redis connection factory
     */
    public RedisLockService(RedisConnectionFactory connectionFactory) {
        this(connectionFactory, DEFAULT_REGISTRY_KEY);
    }
    /**
     * Instantiates a new redis lock service.
     *
     * connectionFactory the redis connection factory
     * @param registryKey The key prefix for locks.
     */
    public RedisLockService(RedisConnectionFactory connectionFactory, String registryKey) {
        this.redisLockRegistry = new RedisLockRegistry(connectionFactory, registryKey);
    }
    /**
     * Instantiates a new redis lock service.
     *
     * @param connectionFactory the redis connection factory
     * @param expireAfter The expiration in milliseconds.
     */
    public RedisLockService(RedisConnectionFactory connectionFactory, long expireAfter) {
        this.redisLockRegistry = new RedisLockRegistry(connectionFactory, DEFAULT_REGISTRY_KEY, expireAfter);
    }
   
    /**
     * Instantiates a new redis lock service.
     *
     * @param connectionFactory the redis connection factory
     * @param registryKey The key prefix for locks.
     * @param expireAfter The expiration in milliseconds.
     */
    public RedisLockService(RedisConnectionFactory connectionFactory, String registryKey, long expireAfter) {
        this.redisLockRegistry = new RedisLockRegistry(connectionFactory, registryKey, expireAfter);
    }
   
    @Override
    public DistributedLock obtain(String lockKey) {
        Lock lock = redisLockRegistry.obtain(lockKey);
        return new DelegatingDistributedLock(lockKey, lock);
    }
}

转载于:https://my.oschina.net/xiaominmin/blog/1599184

你可能感兴趣的文章
DRDS SQL 审计与分析——全面洞察 SQL 之利器
查看>>
微信小程序:模板消息推送实现
查看>>
CodePush自定义更新弹框及下载进度条
查看>>
自己总结的php开发中用到的工具
查看>>
小程序视频或音频自定义可拖拽进度条
查看>>
PHP导出超大的CSV格式的Excel表方案
查看>>
Mac 环境下如何生成Git shh key
查看>>
jenkins 使用磁盘检查插件 disk check plugin
查看>>
使用 Ruby 拓展 Vim
查看>>
centos7下安装LNMP(nginx+PHP7.1.9+mysql5.7)
查看>>
NodeAPI学习之Buffer
查看>>
深入java单例模式
查看>>
create-react-app
查看>>
20170812-XSS跨站脚本攻击
查看>>
Let’s Build |> 使用Elixir,Phoenix和React打造克隆版的Slack(part 1)
查看>>
如何让 StackNaivgator 实现越级回跳
查看>>
工具简述
查看>>
Hbase 集群搭建
查看>>
分布式文件服务器dfs
查看>>
正则表达式
查看>>