Revision ac4f68ab

b/src/gr/ebs/gss/client/rest/GetCommand.java
35 35
import gr.ebs.gss.client.rest.resource.UserResource;
36 36
import gr.ebs.gss.client.rest.resource.UserSearchResource;
37 37

  
38
import java.util.HashMap;
39
import java.util.Map;
40

  
38 41
import com.google.gwt.core.client.GWT;
39 42
import com.google.gwt.http.client.Request;
40 43
import com.google.gwt.http.client.RequestBuilder;
......
52 55
	private String username;
53 56
	private boolean requestSent = false;
54 57
	T cached;
58
	
59
	private static final long MAX_CACHE_AGE = 1000;
60
	
61
	private static class RequestData {
62
		public String path;
63
		public String username;
64
		
65
		public RequestData(String _path, String _username) {
66
			path = _path;
67
			username = _username;
68
		}
69

  
70
		@Override
71
		public int hashCode() {
72
			final int prime = 31;
73
			int result = 1;
74
			result = prime * result + ((path == null) ? 0 : path.hashCode());
75
			result = prime * result + ((username == null) ? 0 : username.hashCode());
76
			return result;
77
		}
78

  
79
		@Override
80
		public boolean equals(Object obj) {
81
			if (this == obj)
82
				return true;
83
			if (obj == null)
84
				return false;
85
			if (getClass() != obj.getClass())
86
				return false;
87
			RequestData other = (RequestData) obj;
88
			if (path == null) {
89
				if (other.path != null)
90
					return false;
91
			} else if (!path.equals(other.path))
92
				return false;
93
			if (username == null) {
94
				if (other.username != null)
95
					return false;
96
			} else if (!username.equals(other.username))
97
				return false;
98
			return true;
99
		}
100
	}
101
	
102
	private static class ResponseData {
103
		public long timestamp;
104
		public Object result;
105
		public ResponseData(long _timestamp, Object _result) {
106
			timestamp = _timestamp;
107
			result = _result;
108
		}
109
	}
110
	
111
	private static Map<RequestData, ResponseData> cache = new HashMap<RequestData, ResponseData>();
112
	
55 113

  
56 114
	public GetCommand(Class<T> theclass, String pathToGet, T theCached){
57 115
		this(theclass, pathToGet, true, theCached);
......
136 194

  
137 195
	@Override
138 196
	public boolean execute() {
139
		if(!requestSent)
140
			sendRequest();
141 197
		boolean com = isComplete();
198
		RequestData key = new RequestData(path, username);
199
		if (!com) {
200
			if (cache.containsKey(key)) {
201
				ResponseData resp = cache.get(key);
202
				if (resp==null) {
203
					return true;
204
				}
205
				GWT.log("Cache hit: "+key.username+", "+key.path, null);
206
				if (System.currentTimeMillis()-resp.timestamp>MAX_CACHE_AGE) {
207
					// Cache stale, remove
208
					cache.put(key,null);
209
				}
210
				else {
211
					// Use cache data
212
					if(isShowLoadingIndicator())
213
						GSS.get().hideLoadingIndicator();
214
					result = (T) resp.result;
215
					if (result != null) {
216
						onComplete();
217
					}
218
					complete = true;
219
					return false;
220
				}
221
			}
222
		
223
			if(!requestSent) {
224
				cache.put(key,null);
225
				sendRequest();
226
			}
227
		}
228
		
142 229
		if(com){
143 230
			if(isShowLoadingIndicator())
144 231
				GSS.get().hideLoadingIndicator();
145
			if(getResult() != null)
232
			if(getResult() != null) {
233
				// Add to cache
234
				cache.put(key, new ResponseData(System.currentTimeMillis(), getResult()));
146 235
				onComplete();
236
			}
147 237
			return false;
148 238
		}
149 239
		return true;

Also available in: Unified diff