@SuppressWarnings("deprecation") // self-use private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException { in.defaultReadObject(); MapMaker mapMaker = readMapMaker(in); delegate = mapMaker.makeComputingMap(computingFunction); readEntries(in); }
private static Map<Pair<Class, String>, String> createResourceCache() { MapMaker maker = new MapMaker().softValues().maximumSize(100); String isDev = System.getProperty(ConstantValues.dev.getPropertyName()); if (StringUtils.isBlank(isDev)) { isDev = System.getProperty("aol.devMode"); } if (Boolean.parseBoolean(isDev)) { maker.expireAfterWrite(30, TimeUnit.SECONDS); } return maker.makeComputingMap( new Function<Pair<Class, String>, String>() { @Override public String apply(@Nonnull Pair<Class, String> pair) { return readResourceNoCache(pair.getFirst(), pair.getSecond()); } }); }
public SocialDataCaches(Duration expiresAfter) { final MapMaker cacheTemplate = new MapMaker() .softValues() .expireAfterWrite(expiresAfter.getMillis(), TimeUnit.MILLISECONDS); perUserCaches = cacheTemplate.makeComputingMap( new Function<UserRef, ConcurrentMap<K, V>>() { @Override public ConcurrentMap<K, V> apply(UserRef from) { return cacheTemplate.makeMap(); } }); anonCache = cacheTemplate.makeMap(); }
@Path("/v2/event") public class EventResource { private final Set<EventWriter> writers; private final Set<String> acceptedEventTypes; private final ScheduledExecutorService executor; private final EventClient eventClient; private final MapMaker maker = new MapMaker(); private ConcurrentMap<String, ProcessStats> stats = maker.makeMap(); @Inject public EventResource( Set<EventWriter> writers, ServerConfig config, ScheduledExecutorService executor, EventClient eventClient) { Preconditions.checkNotNull(writers, "writer must not be null"); this.writers = writers; this.acceptedEventTypes = ImmutableSet.copyOf(config.getAcceptedEventTypes()); this.stats = Maps.newConcurrentMap(); this.executor = executor; this.eventClient = eventClient; } @POST @Consumes(MediaType.APPLICATION_JSON) public Response post(List<Event> events) throws IOException { Set<String> badEvents = Sets.newHashSet(); for (Event event : events) { if (acceptedEventType(event.getType())) { for (EventWriter writer : writers) { writer.write(event); } increment(event.getType()); } else { badEvents.add(event.getType()); } } if (!badEvents.isEmpty()) { String errorMessage = "Invalid event type(s): " + Joiner.on(", ").join(badEvents); return Response.status(Status.BAD_REQUEST).entity(errorMessage).build(); } return Response.status(Response.Status.ACCEPTED).build(); } private boolean acceptedEventType(String type) { return acceptedEventTypes.isEmpty() || acceptedEventTypes.contains(type); } private void increment(String eventType) { ProcessStats eventStats = stats.get(eventType); if (eventStats == null) { stats.putIfAbsent(eventType, new ProcessStats(executor, eventClient, eventType)); eventStats = stats.get(eventType); eventStats.start(); } eventStats.processed(1); } }
/** * Creates a new cache instance. * * @param name a human readable identifier for this cache. Note that this value will be used to * derive a directory name if the disk cache is enabled, so don't get too creative here (camel * case names work great) * @param initialCapacity the initial element size of the cache * @param expirationInMinutes time in minutes after which elements will be purged from the cache * (NOTE: this only affects the memory cache, the disk cache does currently NOT handle element * TTLs!) * @param maxConcurrentThreads how many threads you think may at once access the cache; this need * not be an exact number, but it helps in fragmenting the cache properly */ public AbstractCache( String name, int initialCapacity, long expirationInMinutes, int maxConcurrentThreads) { this.name = name; MapMaker mapMaker = new MapMaker(); mapMaker.initialCapacity(initialCapacity); mapMaker.expiration(expirationInMinutes * 60, TimeUnit.SECONDS); mapMaker.concurrencyLevel(maxConcurrentThreads); mapMaker.softValues(); this.cache = mapMaker.makeMap(); }
/** * Configures the given map maker to use weak keys, if possible; does nothing otherwise (i.e., in * GWT). This is sometimes acceptable, when only server-side code could generate enough volume * that reclamation becomes important. */ static MapMaker tryWeakKeys(MapMaker mapMaker) { return mapMaker.weakKeys(); }
public SessionCache() { final MapMaker mapMaker = new MapMaker(); sessions = mapMaker.makeMap(); sessionCallbackMap = mapMaker.makeMap(); }