summaryrefslogtreecommitdiffstats
path: root/dozentenmodul/src/main/java/org/openslx/dozmod/thrift/cache/ImageCache.java
blob: fdd71dfb4d99d8c58c80a815bc65753dc414c92b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package org.openslx.dozmod.thrift.cache;

import java.util.List;

import org.apache.thrift.TException;
import org.openslx.bwlp.thrift.iface.ImageSummaryRead;
import org.openslx.bwlp.thrift.iface.TAuthorizationException;
import org.openslx.bwlp.thrift.iface.TInvocationException;
import org.openslx.dozmod.thrift.Session;
import org.openslx.thrifthelper.ThriftManager;
import org.openslx.util.GenericDataCache;
import org.openslx.util.GenericDataCache.CacheMode;

public class ImageCache {
	
	private static final int CACHE_TIME_LIST_MS = 30 * 1000;
	
	private static final GenericDataCache<List<ImageSummaryRead>> listCache = new GenericDataCache<List<ImageSummaryRead>>(CACHE_TIME_LIST_MS) {
		@Override
		protected List<ImageSummaryRead> update() throws TAuthorizationException, TInvocationException, TException {
			List<ImageSummaryRead> result = null;
			int pageSize = Session.getSatelliteConfig().pageSize;
			for (int i = 0;; ++i) {
				List<ImageSummaryRead> page = ThriftManager.getSatClient().getImageList(Session.getSatelliteToken(), null, i);
				if (result == null) {
					result = page;
				} else {
					result.addAll(page);
				}
				if (page.size() < pageSize)
					break;
			}
			return result;
		}
	};
	
	public static List<ImageSummaryRead> get(boolean forceRefresh) {
		return listCache.get(forceRefresh ? CacheMode.NEVER_CACHED : CacheMode.DEFAULT);
	}
	
}