KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > db > impl > SimpleCache


1 /*
2  * (c) Copyright 2002, 2003, 2004, 2005 Hewlett-Packard Development Company, LP
3  * All rights reserved.
4  *
5  *
6  */

7
8
9 //=======================================================================
10
// Package
11
package com.hp.hpl.jena.db.impl;
12
13 //=======================================================================
14
// Imports
15
import java.util.*;
16
17 import com.hp.hpl.jena.util.CollectionFactory;
18
19
20 //=======================================================================
21
/**
22 * Trivial implementation of the generic cache interface used to cache
23 * literals and resources. This implementation simple flushes the cache
24 * when the threshold limit is exceeded.
25 *
26 * @author <a HREF="mailto:der@hplb.hpl.hp.com">Dave Reynolds</a>
27 * @version $Revision: 1.5 $ on $Date: 2005/02/21 12:03:12 $
28 */

29
30 public class SimpleCache implements ICache {
31
32     /** The cache itself */
33     protected Map cache = CollectionFactory.createHashedMap();
34
35     /** The current size limit */
36     protected int threshold;
37
38     /** The current number of entries (probably redundant, just use cache.size) */
39     protected int count = 0;
40
41     /**
42      * Create an empty cache with the given threshold limit.
43      *
44      * @param threshold the cache size limit, use 0 for no cache, -1 for
45      * unlimited cache growth; any other number indicates the number of cache entries
46      */

47     public SimpleCache(int threshold) {
48         this.threshold = threshold;
49     }
50
51     /**
52      * Add an entry to the cache
53      * @param id the database ID to be used as an index
54      * @param val the literal or resources to be stored
55      */

56     public void put(IDBID id, Object JavaDoc val) {
57         if (threshold == 0) return;
58         if (threshold > 0 && count >= threshold) {
59             cache = CollectionFactory.createHashedMap();
60             count = 0;
61         }
62         count++;
63         cache.put(id, val);
64     }
65
66     /**
67      * Retreive an object from the cache
68      * @param id the database ID of the object to be retrieved
69      * @return the object or null if it is not in the cache
70      */

71     public Object JavaDoc get(IDBID id) {
72         return cache.get(id);
73     }
74
75     /**
76      * Set a threshold for the cache size in terms of the count of cache entries.
77      * For literals a storage limit rather than a count might be more useful but
78      * counts are easier, more general and sufficient for the current use.
79      *
80      * @param threshold the cache size limit, use 0 for no cache, -1 for
81      * unlimited cache growth; any other number indicates the number of cache entries
82      */

83     public void setLimit(int threshold) {
84         this.threshold = threshold;
85         if (threshold >= 0 && count > threshold) {
86             cache = CollectionFactory.createHashedMap();
87             count = 0;
88         }
89     }
90
91     /**
92      * Return the current threshold limit for the cache size.
93      */

94     public int getLimit() {
95         return threshold;
96     }
97 }
98 /*
99  * (c) Copyright 2002, 2003, 2004, 2005 Hewlett-Packard Development Company, LP
100  * All rights reserved.
101  *
102  * Redistribution and use in source and binary forms, with or without
103  * modification, are permitted provided that the following conditions
104  * are met:
105  * 1. Redistributions of source code must retain the above copyright
106  * notice, this list of conditions and the following disclaimer.
107  * 2. Redistributions in binary form must reproduce the above copyright
108  * notice, this list of conditions and the following disclaimer in the
109  * documentation and/or other materials provided with the distribution.
110  * 3. The name of the author may not be used to endorse or promote products
111  * derived from this software without specific prior written permission.
112  *
113  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
114  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
115  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
116  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
117  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
118  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
119  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
120  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
121  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
122  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
123  */

124
125
126
Popular Tags