001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029:
030: package com.caucho.ejb.entity;
031:
032: import java.util.*;
033: import java.util.logging.*;
034:
035: import com.caucho.util.*;
036:
037: /**
038: * Caches entities
039: */
040: public class EntityCache {
041: private static final L10N L = new L10N(EntityCache.class);
042: protected static final Logger log = Logger
043: .getLogger(EntityCache.class.getName());
044:
045: private int _entityCacheSize = 32 * 1024;
046:
047: private long _entityCacheTimeout = 5000L;
048:
049: private LruCache<EntityKey, QEntityContext> _entityCache;
050:
051: private EntityKey _entityKey = new EntityKey();
052:
053: /**
054: * Caches entity beans
055: */
056: public EntityCache() {
057: }
058:
059: /**
060: * Gets the cache timeout.
061: */
062: public long getCacheTimeout() {
063: return _entityCacheTimeout;
064: }
065:
066: /**
067: * Sets the cache timeout.
068: */
069: public void setCacheTimeout(long cacheTimeout) {
070: _entityCacheTimeout = cacheTimeout;
071: // _amberPersistenceUnitenceUnitenceUnit.setTableCacheTimeout(cacheTimeout);
072: }
073:
074: /**
075: * Gets the cache size.
076: */
077: public int getCacheSize() {
078: return _entityCacheSize;
079: }
080:
081: /**
082: * Sets the cache size.
083: */
084: public void setCacheSize(int cacheSize) {
085: _entityCacheSize = cacheSize;
086: }
087:
088: /**
089: * Initialize the manager after all the configuration files have been read.
090: */
091: public void start() {
092: if (_entityCache == null)
093: _entityCache = new LruCache<EntityKey, QEntityContext>(
094: _entityCacheSize);
095: }
096:
097: /**
098: * Adds a new entity.
099: */
100: public QEntityContext getEntity(EntityServer server, Object key) {
101: synchronized (_entityKey) {
102: _entityKey.init(server, key);
103:
104: return _entityCache.get(_entityKey);
105: }
106: }
107:
108: /**
109: * Adds a new entity.
110: */
111: public QEntityContext putEntityIfNew(EntityServer server,
112: Object key, QEntityContext context) {
113: return _entityCache.putIfNew(new EntityKey(server, key),
114: context);
115: }
116:
117: /**
118: * Adds a new entity.
119: */
120: public void removeEntity(EntityServer server, Object key) {
121: if (_entityCache == null)
122: return;
123:
124: synchronized (_entityKey) {
125: _entityKey.init(server, key);
126: _entityCache.remove(_entityKey);
127: }
128: }
129:
130: /**
131: * Adds a new entity.
132: */
133: public void removeBeans(ArrayList<QEntityContext> beans,
134: EntityServer server) {
135: if (_entityCache == null)
136: return;
137:
138: synchronized (_entityCache) {
139: Iterator<LruCache.Entry<EntityKey, QEntityContext>> iter;
140:
141: iter = _entityCache.iterator();
142:
143: while (iter.hasNext()) {
144: LruCache.Entry<EntityKey, QEntityContext> entry = iter
145: .next();
146:
147: beans.add(entry.getValue());
148:
149: iter.remove();
150: }
151: }
152: }
153:
154: /**
155: * Invalidates the caches for all the beans.
156: */
157: public void invalidateCache() {
158: }
159: }
|