Ejemplo n.º 1
0
  /**
   * Performs get request. Checks cache first. Updates cache if not in cache but located in store.
   *
   * @param key String key
   * @return String value associated with key
   * @throws KVException with ERROR_NO_SUCH_KEY if key does not exist in store
   */
  @Override
  public String get(String key) throws KVException {
    if (key.length() > MAX_KEY_SIZE) {
      KVMessage msg = new KVMessage(KVConstants.RESP, ERROR_NO_SUCH_KEY);
      throw new KVException(msg);
    }

    Lock lock = dataCache.getLock(key);
    String ret = null;
    try {
      lock.lock();
      ret = dataCache.get(key);
      if (ret == null) {
        ret = dataStore.get(key);
        if (ret != null) dataCache.put(key, ret);
      }
    } finally {
      lock.unlock();
    }
    return ret;
  }