1

この Java キャッシュはより安全または高速になりますか?

public class Cache {
    private long lastupdateTime = 0L;
    private String cachedData = null;
    public String getData() {
        long now = System.currentTimeMillis();
        if (now - lastupdateTime < 30000) {
            return cachedData;
        }
        else {
            synchronized (this) {
                if (now - lastupdateTime < 30000) {
                    return cachedData;
                }
                else {
                    lastupdateTime = now;
                    cachedData = getDataFromDatabase();
                    return cachedData;
                }
            }
        }
    }
    private String getDataFromDatabase() { ... }
}
4

3 に答える 3

2

はい。作っていないことlastupdateTimecachedData気まぐれであることを無視しても、

                lastupdateTime = now;
                cachedData = getDataFromDatabase();

故障しています。
例外で失敗した場合getDataFromDatabaseは、既に更新されているlastupdateTimeため、古いデータを 30 秒間返し続け、最初の試行で失敗したnull場合は戻る可能性があります。getDataFromDatabase

lastUpdateTimeに初期化しても何も失われずLong.MIN_VALUE、クロックが後方に設定されているシステムでより確実に動作します。

System.getCurrentTimeMillis30 秒のキャッシュに影響を与える可能性は低いですが、この使用例では、代わりに使用しない理由はありませんSystem.nanoTime()

于 2013-08-28T21:58:26.400 に答える
1

ここにアイデアがあります -synchronized古風で非効率的なメカニズムである原則に基づいています。

ここでは、 を使用しAtomicReference<Phaser>て、キャッシュが更新中であることを示します。を使用して、更新のPhaser完了を待つことができます。

public class Test {

  public static class Cache {
    // Cache timeout.
    private static final long Timeout = 10000;
    // Last time we updated.
    private volatile long lastupdateTime = 0L;
    // The cached data.
    private volatile String cachedData = null;
    // Cache is in the progress of updating.
    private AtomicReference<Phaser> updating = new AtomicReference<>();
    // The next Phaser to use - avoids unnecessary Phaser creation.
    private Phaser nextPhaser = new Phaser(1);

    public String getData() {
      // Do this just once.
      long now = System.currentTimeMillis();
      // Watch for expiry.
      if (now - lastupdateTime > Timeout) {
        // Make sure only one thread updates.
        if (updating.compareAndSet(null, nextPhaser)) {
          // We are the unique thread that gets to do the updating.
          System.out.println(Thread.currentThread().getName() + " - Get ...");
          // Get my new cache data.
          cachedData = getDataFromDatabase();
          lastupdateTime = now;
          // Make the Phaser to use next time - avoids unnecessary creations.
          nextPhaser = new Phaser(1);
          // Get the queue and clear it - there cannot be any new joiners after this.
          Phaser queue = updating.getAndSet(null);
          // Inform everyone who is waiting that they can go.
          queue.arriveAndDeregister();
        } else {
          // Wait in the queue.
          Phaser queue = updating.get();
          if (queue != null) {
            // Wait for it.
            queue.register();
            System.out.println(Thread.currentThread().getName() + " - Waiting ...");
            queue.arriveAndAwaitAdvance();
            System.out.println(Thread.currentThread().getName() + " - Back");
          }
        }
      }
      // Let them have the correct data.
      return cachedData;
    }

    private String getDataFromDatabase() {
      try {
        // Deliberately wait for a bit.
        Thread.sleep(5000);
      } catch (InterruptedException ex) {
        // Ignore.
      }
      System.out.println(Thread.currentThread().getName() + " - Hello");
      return "Hello";
    }
  }

  public void test() throws InterruptedException {
    System.out.println("Hello");
    // Start time.
    final long start = System.currentTimeMillis();
    // Make a Cache.
    final Cache c = new Cache();
    // Make some threads.
    for (int i = 0; i < 20; i++) {
      Thread t = new Thread(new Runnable() {
        @Override
        public void run() {
          while (System.currentTimeMillis() - start < 60000) {
            c.getData();
            try {
              Thread.sleep(500);
            } catch (InterruptedException ex) {
              // Ignore.
            }
          }
        }
      });
      t.setName("Thread - " + i);
      t.start();
      // Stagger the threads.
      Thread.sleep(300);
    }
  }

  public static void main(String args[]) throws InterruptedException {
    new Test().test();
  }
}

を使用したのはこれが初めてであることに注意してくださいPhaser-うまくいけばいいのですが。

タイムアウトがデータベースからデータを取得するのにかかる時間よりも長い場合、このアルゴリズムが機能しなくなる可能性があることに注意してください。

于 2013-08-28T22:03:40.647 に答える