Skip to content

PYTech/CacheLock

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

31 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

CacheLock

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.

Figure_01

Licensed under the Apache License 2.0.

Features:

  • 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

Recent Releases:

####Trunk: current development branch.

####17-July 2015 - version 1.0.0 released First stable version.

Usage:

How to Include:

  1. Download this jar file and include it in your project, and then find the following dependent libs manually:
  2. Use maven to import this lib (see the instructions below).
  3. Download the source code and customize it according to your need.

Maven

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>

Coding Samples:

  1. 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);
   }
  1. 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);
   }

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Java 100.0%