summaryrefslogtreecommitdiffstats
path: root/dozentenmodul/src/main/java/org/openslx/dozmod/thrift/cache/ImagePublishedCache.java
blob: 39ca3be71e6f17a4923378595d492e676c2b36e6 (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
42
43
44
45
46
47
48
49
50
51
package org.openslx.dozmod.thrift.cache;

import java.util.List;

import org.apache.log4j.Logger;
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 ImagePublishedCache {

	private final static Logger LOGGER = Logger
			.getLogger(ImagePublishedCache.class);

	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 = null;
				page = ThriftManager.getMasterClient().getPublicImages(
						Session.getMasterToken(), 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);
	}
}