cachedob.jav

来自「有完整的程序和源码」· JAV 代码 · 共 121 行

JAV
121
字号
//---------------------------------------------------------
// Application: Personal of System
// Author     : eSingle
// File       : CachedObject.java
//
// Copyright 2002 LandSoft Corp.
// Generated at Mon Nov 18 10:18:19 CST 2002
// Created by caoguangxin
// mailto:gxcao@mail.tsinghua.edu.cn
//---------------------------------------------------------

package com.landsoft.personal.util;

import java.util.*;

public class CachedObject {

  public Object object = null;
  private Date dateofExpiration = null;
  private String identifier = null;
  private Date lastAccessTime = new Date();
  private long numAccess = 1;
  private int size;

  // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

  public CachedObject(Object obj, String id, int minutesToLive) {
    this.object = obj;
    this.identifier = id;

    size = objectSize(obj);
    lastAccessTime = new Date();
    // minutesToLive of 0 means it lives on indefinitely.
    if (minutesToLive != 0) {
      Calendar cal = Calendar.getInstance();
      cal.setTime(lastAccessTime);
      cal.add(cal.MINUTE, minutesToLive);
      dateofExpiration = cal.getTime();
    }
  }

  public void setLastAccessTime(Date lastAccessTime) {
    this.lastAccessTime = lastAccessTime;
  }

  public boolean isExpired() {
    // Remember if the minutes to live is zero then it lives forever!
    if (dateofExpiration != null && dateofExpiration.before(new Date())) {
      return true;
    }
    return false;
  }

  public String getIdentifier() {
    return identifier;
  }

  public Object getObject() {
    return object;
  }

  public Date getDateofExpiration() {
    return (this.dateofExpiration);
  }

  public Date getLastAccessTime() {
    return (this.lastAccessTime);
  }

  public long getNumAccess() {
    return (this.numAccess);
  }

  public long getSize() {
    return (this.size);
  }

  public double getMixCost() {
    long milis = new Date().getTime() - lastAccessTime.getTime();
    if(milis == 0) {
      milis = 1;
    }
    return (double)numAccess / (double)milis / (double)size;
  }

  public double getLRUCost() {
    long milis = new Date().getTime() - lastAccessTime.getTime();
    if(milis == 0) {
      milis = 1;
    }
    return 1.0/(double)milis;
  }

  public double getLFUCost() {
    return (double) numAccess;
  }

  public void incNumAccess() {
    numAccess++;
  }

  public boolean equals(Object o2) {
    try {
      String key2 = ((CachedObject) o2).getIdentifier();
      return identifier.equals(key2);
    } catch (Exception e) {
      return false;
    }
  }

  private static int objectSize(Object o) {
    try {
      int size = ((List) o).size();
      return size + 1;
    } catch (Exception e) {
      return 1;
    }
  }

}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?