Package mxp.lucene.store

Source Code of mxp.lucene.store.RedisLock

package mxp.lucene.store;

import java.io.IOException;

import org.apache.lucene.store.Lock;

import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.ShardedJedisPool;

public class RedisLock extends Lock {
 
  String name;
  ShardedJedisPool pool;
 
  public RedisLock(String nm, ShardedJedisPool pl) {
    name = nm;
    pool = pl;
  }

  @Override
  public boolean isLocked() throws IOException {
    ShardedJedis jds = pool.getResource();
    boolean ret = jds.exists(name.concat(".lock"));
    pool.returnResource(jds);
    return ret;
  }

  @Override
  public boolean obtain() throws IOException {
    if( isLocked() )
      return false;
    ShardedJedis jds = pool.getResource();
    String ret = jds.set(name+".lock", "1");
    pool.returnResource(jds);
    return ret != null;
  }

  @Override
  public void release() throws IOException {
    ShardedJedis jds = pool.getResource();
    jds.del(name+".lock");
    pool.returnResource(jds);
  }

}
TOP

Related Classes of mxp.lucene.store.RedisLock

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.