示例#1
0
 public void remove(String collection, DBObject query) {
   DBCollection dbCollection = getCollection(collection);
   dbCollection.remove(query);
   Jedis jedis = JedisManager.getJedis();
   jedis.zrem("expire", query.toString());
   jedis.hdel(collection, query.toString());
   JedisManager.returnJedis(jedis);
 }
示例#2
0
 public void updateDocument(String collection, DBObject query, DBObject document) {
   DBCollection dbCollection = getCollection(collection);
   dbCollection.update(query, document);
   Jedis jedis = JedisManager.getJedis();
   if (jedis.hexists(collection, query.toString())) {
     jedis.hset(collection, query.toString(), findOneNoCache(collection, query).toString());
     jedis.zadd("expire", System.currentTimeMillis() + 300000, collection + "_" + query);
   }
   JedisManager.returnJedis(jedis);
 }
示例#3
0
 public void delete(String collection, DBObject query) {
   DBCollection dbCollection = getCollection(collection);
   dbCollection.remove(query);
   Jedis jedis = JedisManager.getJedis();
   if (jedis.hexists(collection, query.toString())) {
     jedis.hdel(collection, query.toString(), findOneNoCache(collection, query).toString());
     jedis.zrem("expire", collection + "_" + query);
   }
   JedisManager.returnJedis(jedis);
 }
示例#4
0
  public DBObject findOne(String collection, DBObject query) {
    Jedis jedis = JedisManager.getJedis();

    Set<String> expiredKeys = jedis.zrangeByScore("expire", 0, System.currentTimeMillis());
    if (expiredKeys.isEmpty() == false) {
      String[] keyArray = new String[expiredKeys.size()];
      expiredKeys.toArray(keyArray);
      jedis.del(keyArray);
      jedis.zrem("expire", keyArray);
    }

    JSONObject jsonObject = null;
    if (jedis.hexists(collection, query.toString())) {
      String data = jedis.hget(collection, query.toString());
      if (data != null) {
        try {
          jsonObject = new JSONObject(data);
        } catch (JSONException e) {
          e.printStackTrace();
        }
      }
    }
    DBObject dbObject;
    if (jsonObject == null) {
      DBCollection dbCollection = getCollection(collection);
      DBCursor dbCursor = dbCollection.find(query).limit(1);
      if (dbCursor.hasNext() == false) {
        return null;
      }
      dbObject = dbCursor.next();
      dbCursor.close();

      jedis.hset(collection, query.toString(), dbObject.toString());
      jedis.zadd("expire", System.currentTimeMillis() + 300000, collection + "_" + query);
    } else {
      dbObject = (DBObject) JSON.parse(jsonObject.toString());
      jedis.hset(collection, query.toString(), dbObject.toString());
      jedis.zadd("expire", System.currentTimeMillis() + 300000, collection + "_" + query);
    }

    JedisManager.returnJedis(jedis);
    return dbObject;
  }