Cache Lock
is a distributed and scalable Java lock management tool which can support almost all
kinds of cache server.
It supports Redis and memcached cache servers by default, based on Jedis and spymemcached client libraries. Other types of cache servers and client libraries can be easily supported by customized classes implementation.
Licensed under the Apache License 2.0.
- Distributed implementation of reentrant
java.util.concurrent.locks.Lock
- Support
Redis and memcached servers
by default. - Support
Jedis and spymemcached libraries
by default. - Support
timeout
mechanism for target lock acquisition - Support
TTL
setting for locks' existence on cache server Dynamic interval algorithm
to deal with starvation issue- Support
all
kinds of cache client library with simple client class implementation - Support
all
kinds of cache server with simple corresponding client class implementation. - Thread-safe implementation
####Trunk: current development branch.
####17-July 2015 - version 1.0.0 released First stable version.
- Download this jar file and include it in your project, and then
find the following dependent libs manually
: - Use
maven
to import this lib (see the instructions below). - Download the source code and customize it according to your need.
Include the following script to your pom.xml
dependency list:
(Central Repo under Application...)
<dependency>
<groupId>xxx</groupId>
<artifactId>xxx</artifactId>
<version>1.0.0</version>
</dependency>
- Use Jedis lib to acquire lock for key "target" from Redis cache server on host "1.2.3.4" and port 6379:
// Generate cache client
Jedis jedis = new Jedis("1.2.3.4", 6379);
RedisClient client = new RedisClient(jedis);
// Generate cache lock obj
CacheLock lock = new RedisLock("target", client);
// Use locker to lock/unlock target
LockSmith locker = new LockSmith();
locker.lock(lock);
try {
// Operations...
......
} finally {
locker.unlock(lock);
}
- Use Jedis lib to acquire lock for key "lockit" and field "tField" from Redis cache cluster on hosts "192.168.1.1", "192.168.1.2", "192.168.1.3", "192.168.1.4" and port 6379:
// Generate jedis cluster client
Set<HostAndPort> nodes = new HashSet<HostAndPort>();
nodes.add(new HostAndPort(192.168.1.1, 6379));
nodes.add(new HostAndPort(192.168.1.2, 6379));
nodes.add(new HostAndPort(192.168.1.3, 6379));
nodes.add(new HostAndPort(192.168.1.4, 6379));
JedisCluster cluster = new JedisCluster(nodes);
RedisClusterClient client = new RedisClusterClient(cluster);
// Generate cache lock obj
CacheLock lock = new RedisClusterLock("lockit", "tField", client);
// Use locker to lock/unlock target
LockSmith locker = new LockSmith();
locker.lock(lock);
try {
// Operations...
......
} finally {
locker.unlock(lock);
}