⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cachedobject.java

📁 本例采用sql server数据库。基于JBuilder的tomcat上运行。访问数据库基于struts的datasources.里面有详细的操作和设置说明。
💻 JAVA
字号:
//---------------------------------------------------------
// Application: Gsm of Application
// Author     : esingle
// File       : CachedObject.java
//
// Copyright 2004 landsoft corp
// Generated at Wed Mar 10 15:35:57 CST 2004
// created by 曹广鑫
// mailto:gxcao@mail.tsinghua.edu.cn
//---------------------------------------------------------

package com.landsoft.gsm.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 + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -