commit | author | age
|
4d44cf
|
1 |
/*
|
JM |
2 |
* Copyright 2011 gitblit.com.
|
|
3 |
*
|
|
4 |
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5 |
* you may not use this file except in compliance with the License.
|
|
6 |
* You may obtain a copy of the License at
|
|
7 |
*
|
|
8 |
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9 |
*
|
|
10 |
* Unless required by applicable law or agreed to in writing, software
|
|
11 |
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13 |
* See the License for the specific language governing permissions and
|
|
14 |
* limitations under the License.
|
|
15 |
*/
|
9275bd
|
16 |
package com.gitblit.utils;
|
4d44cf
|
17 |
|
JM |
18 |
import java.io.Serializable;
|
|
19 |
import java.util.Date;
|
|
20 |
import java.util.Map;
|
|
21 |
import java.util.concurrent.ConcurrentHashMap;
|
|
22 |
|
|
23 |
/**
|
9275bd
|
24 |
* Reusable coarse date-based object cache. The date precision is in
|
JM |
25 |
* milliseconds and in fast, concurrent systems this cache is too simplistic.
|
|
26 |
* However, for the cases where its being used in Gitblit this cache technique
|
|
27 |
* is just fine.
|
4d44cf
|
28 |
*
|
JM |
29 |
* @author James Moger
|
|
30 |
*
|
|
31 |
*/
|
|
32 |
public class ObjectCache<X> implements Serializable {
|
|
33 |
|
|
34 |
private static final long serialVersionUID = 1L;
|
|
35 |
|
|
36 |
private final Map<String, CachedObject<X>> cache = new ConcurrentHashMap<String, CachedObject<X>>();
|
|
37 |
|
|
38 |
private class CachedObject<Y> {
|
|
39 |
|
|
40 |
public final String name;
|
|
41 |
|
|
42 |
private volatile Date date;
|
|
43 |
|
|
44 |
private volatile Y object;
|
|
45 |
|
|
46 |
CachedObject(String name) {
|
|
47 |
this.name = name;
|
|
48 |
date = new Date(0);
|
|
49 |
}
|
|
50 |
|
|
51 |
@Override
|
|
52 |
public String toString() {
|
|
53 |
return getClass().getSimpleName() + ": " + name;
|
|
54 |
}
|
|
55 |
}
|
|
56 |
|
|
57 |
public boolean hasCurrent(String name, Date date) {
|
|
58 |
return cache.containsKey(name) && cache.get(name).date.compareTo(date) == 0;
|
|
59 |
}
|
|
60 |
|
|
61 |
public Date getDate(String name) {
|
|
62 |
return cache.get(name).date;
|
|
63 |
}
|
|
64 |
|
|
65 |
public X getObject(String name) {
|
9275bd
|
66 |
if (cache.containsKey(name)) {
|
JM |
67 |
return cache.get(name).object;
|
|
68 |
}
|
|
69 |
return null;
|
4d44cf
|
70 |
}
|
JM |
71 |
|
|
72 |
public void updateObject(String name, X object) {
|
|
73 |
this.updateObject(name, new Date(), object);
|
|
74 |
}
|
|
75 |
|
|
76 |
public void updateObject(String name, Date date, X object) {
|
|
77 |
CachedObject<X> obj;
|
|
78 |
if (cache.containsKey(name)) {
|
|
79 |
obj = cache.get(name);
|
|
80 |
} else {
|
|
81 |
obj = new CachedObject<X>(name);
|
|
82 |
cache.put(name, obj);
|
|
83 |
}
|
|
84 |
obj.date = date;
|
|
85 |
obj.object = object;
|
|
86 |
}
|
|
87 |
|
|
88 |
public Object remove(String name) {
|
4a2868
|
89 |
if (cache.containsKey(name)) {
|
JM |
90 |
return cache.remove(name).object;
|
|
91 |
}
|
|
92 |
return null;
|
4d44cf
|
93 |
}
|
fee060
|
94 |
|
JM |
95 |
public int size() {
|
|
96 |
return cache.size();
|
|
97 |
}
|
4d44cf
|
98 |
}
|