100求实现数据缓存的代码:

birditren2001 2003-10-20 02:47:55
比如说我用socket收到一条信息,先放到缓存中,然后处理数据的程序从缓存中读取数据,经过处理,然后插入到数据库中,请问该如何实现这种缓存机制?好像要用到链表或者队列什么的。
...全文
52 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
birditren2001 2003-10-21
  • 打赏
  • 举报
回复
不知道我的问题用JMS是否可以解决?
listlike 2003-10-21
  • 打赏
  • 举报
回复
去这里看看
http://jakarta.apache.org/commons/pool/
birditren2001 2003-10-21
  • 打赏
  • 举报
回复
楼上是指free的中间件吗?各位高手帮忙。
etre 2003-10-21
  • 打赏
  • 举报
回复
java的缓存机制我看到网上有free的,做的很好的。我不知道在那里了,你去看看吧
birditren2001 2003-10-21
  • 打赏
  • 举报
回复
楼上的大哥:
你的代码还没来得及研究,我只是在JB中新建一个项目,然后新建class,copy了你的代码,
然后把首行package修改成我自己的package,但是程序throws CacheException的地方都标记为红色错误标记,怎么回事?错误信息:"Cache.java": cannot resolve symbol: class CacheException in interface smscache.Cache at line 16, column 38 还有,CachedItem.java中,freshTimestamp = Timestamper.next();和unlockTimestamp = Timestamper.next();这两行都有红色错误标记,错误信息是:"CachedItem.java": cannot resolve symbol: variable Timestamper in class smscache.CachedItem at line 22, column 18
水电费123343 2003-10-21
  • 打赏
  • 举报
回复
//$Id: CacheConcurrencyStrategy.java,v 1.4 2003/05/20 12:56:08 oneovthafew Exp $
package net.sf.hibernate.cache;

/**
* Implementors manage transactional access to cached data. Transactions
* pass in a timestamp indicating transaction start time.
*/
public interface CacheConcurrencyStrategy {

/**
* Attempt to retrieve an object from the cache
* @param key
* @param txTimestamp a timestamp prior to the transaction start time
* @return the cached object or <tt>null</tt>
* @throws CacheException
*/
public Object get(Object key, long txTimestamp) throws CacheException;
/**
* Attempt to cache an object
* @param key
* @param value
* @param txTimestamp a timestamp prior to the transaction start time
* @return <tt>true</tt> if the object was successfully cached
* @throws CacheException
*/
public boolean put(Object key, Object value, long txTimestamp) throws CacheException;
/**
* We are going to attempt to update/delete the keyed object
* @param key
* @throws CacheException
*/
public void lock(Object key) throws CacheException;
/**
* We have finished the attempted update/delete (which may or may
* not have been successful).
* @param key
* @throws CacheException
*/
public void release(Object key) throws CacheException;
public void remove(Object key) throws CacheException;
public void clear() throws CacheException;
public void destroy();
}






水电费123343 2003-10-21
  • 打赏
  • 举报
回复
//$Id: CachedItem.java,v 1.3 2003/01/05 02:11:20 oneovthafew Exp $
package net.sf.hibernate.cache;

import java.io.Serializable;

/**
* An item of cached data, timestamped with the time it was cached,
* when it was locked, when it was unlocked.
* @see ReadWriteCache
*/
public final class CachedItem implements Serializable {

private long freshTimestamp;
private boolean fresh;
private long unlockTimestamp;
private int lock;
private Object value;

public CachedItem(Object value) {
this.value = value;
freshTimestamp = Timestamper.next();
fresh = true;
unlockTimestamp = -1;
}

public long getFreshTimestamp() {
return freshTimestamp;
}

public long getUnlockTimestamp() {
return unlockTimestamp;
}

public Object getValue() {
return value;
}

public boolean isFresh() {
return fresh;
}

public void lock() {
if ( 0 == lock++ ) {
fresh = false;
value = null;
}
}
public void unlock() {
if ( --lock == 0 ) {
unlockTimestamp = Timestamper.next();
}
}
public boolean isUnlocked() {
return lock==0;
}

}






水电费123343 2003-10-21
  • 打赏
  • 举报
回复
//$Id: Cache.java,v 1.5 2003/05/20 12:56:07 oneovthafew Exp $
package net.sf.hibernate.cache;

/**
* Implementors define a caching algorithm. All implementors
* <b>must</b> be threadsafe.
*/
public interface Cache {
/**
* Get an item from the cache
* @param key
* @return the cached object or <tt>null</tt>
* @throws CacheException
*/
public Object get(Object key) throws CacheException;
/**
* Add an item to the cache
* @param key
* @param value
* @throws CacheException
*/
public void put(Object key, Object value) throws CacheException;
/**
* Remove an item from the cache
*/
public void remove(Object key) throws CacheException;
/**
* Clear the cache
*/
public void clear() throws CacheException;
/**
* Clean up
*/
public void destroy() throws CacheException;
/**
* Set the cache region name
* @param regionName
* @throws CacheException
*/
public void setRegion(String regionName) throws CacheException;
}






birditren2001 2003-10-21
  • 打赏
  • 举报
回复
继续顶
birditren2001 2003-10-20
  • 打赏
  • 举报
回复
顶一下。

67,512

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧