<commit_before>package com.pr0gramm.app.services;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.net.Uri;
import android.support.annotation.Nullable;

import com.google.common.base.Optional;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.google.common.collect.ImmutableList;
import com.google.common.io.BaseEncoding;
import com.google.common.primitives.Longs;
import com.pr0gramm.app.api.meta.MetaApi;
import com.pr0gramm.app.api.meta.MetaApi.SizeInfo;
import com.pr0gramm.app.api.pr0gramm.response.Tag;
import com.pr0gramm.app.feed.ContentType;
import com.pr0gramm.app.feed.FeedItem;
import com.pr0gramm.app.ui.PreviewInfo;
import com.pr0gramm.app.util.AndroidUtility;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;

import javax.inject.Inject;
import javax.inject.Singleton;

import gnu.trove.impl.sync.TSynchronizedLongObjectMap;
import gnu.trove.map.TLongObjectMap;
import gnu.trove.map.hash.TLongObjectHashMap;

import static com.pr0gramm.app.util.AndroidUtility.checkNotMainThread;

/**
 * This service helps to locally cache deltas to the pr0gramm. Those
 * deltas might arise because of cha0s own caching.
 */
@Singleton
public class InMemoryCacheService {
    private static final Logger logger = LoggerFactory.getLogger("LocalCacheService");

    private final Cache<Long, ImmutableList<Tag>> tagsCache = CacheBuilder.newBuilder()
            .expireAfterAccess(5, TimeUnit.MINUTES)
            .build();

    private final TLongObjectMap<SizeInfo> sizeInfoCache = new TSynchronizedLongObjectMap<>(
            new TLongObjectHashMap<>());

    private final Cache<Long, Bitmap> lowQualityPreviewCache = CacheBuilder.newBuilder()
            .maximumSize(5_000)
            .build();

    private final Cache<String, EnhancedUserInfo> userInfoCache = CacheBuilder.newBuilder()
            .expireAfterWrite(10, TimeUnit.MINUTES)
            .build();

    private final AtomicReference<long[]> repostCache = new AtomicReference<>(new long[0]);
    private final UriHelper uriHelper;

    @Inject
    public InMemoryCacheService(Context context) {
        this.uriHelper = UriHelper.of(context);
    }

    /**
     * Caches (or enhanced) a list of tags for the given itemId.
     *<commit_msg>#123 Reduce user info cache timeout to 2 minutes.<commit_after>package com.pr0gramm.app.services;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.net.Uri;
import android.support.annotation.Nullable;

import com.google.common.base.Optional;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.google.common.collect.ImmutableList;
import com.google.common.io.BaseEncoding;
import com.google.common.primitives.Longs;
import com.pr0gramm.app.api.meta.MetaApi;
import com.pr0gramm.app.api.meta.MetaApi.SizeInfo;
import com.pr0gramm.app.api.pr0gramm.response.Tag;
import com.pr0gramm.app.feed.ContentType;
import com.pr0gramm.app.feed.FeedItem;
import com.pr0gramm.app.ui.PreviewInfo;
import com.pr0gramm.app.util.AndroidUtility;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;

import javax.inject.Inject;
import javax.inject.Singleton;

import gnu.trove.impl.sync.TSynchronizedLongObjectMap;
import gnu.trove.map.TLongObjectMap;
import gnu.trove.map.hash.TLongObjectHashMap;

import static com.pr0gramm.app.util.AndroidUtility.checkNotMainThread;

/**
 * This service helps to locally cache deltas to the pr0gramm. Those
 * deltas might arise because of cha0s own caching.
 */
@Singleton
public class InMemoryCacheService {
    private static final Logger logger = LoggerFactory.getLogger("LocalCacheService");

    private final Cache<Long, ImmutableList<Tag>> tagsCache = CacheBuilder.newBuilder()
            .expireAfterAccess(5, TimeUnit.MINUTES)
            .build();

    private final TLongObjectMap<SizeInfo> sizeInfoCache = new TSynchronizedLongObjectMap<>(
            new TLongObjectHashMap<>());

    private final Cache<Long, Bitmap> lowQualityPreviewCache = CacheBuilder.newBuilder()
            .maximumSize(5_000)
            .build();

    private final Cache<String, EnhancedUserInfo> userInfoCache = CacheBuilder.newBuilder()
            .expireAfterWrite(2, TimeUnit.MINUTES)
            .build();

    private final AtomicReference<long[]> repostCache = new AtomicReference<>(new long[0]);
    private final UriHelper uriHelper;

    @Inject
    public InMemoryCacheService(Context context) {
        this.uriHelper = UriHelper.of(context);
    }

    /**
     * Caches (or enhanced) a list of tags for the given itemId.
     *
{'fraction_non_alphanumeric': 0.10174134220309138, 'fraction_numerical': 0.00919585208374095, 'mean_word_length': 4.3584905660377355, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2277430', 'n_tokens_mistral': 1601, 'n_tokens_neox': 1570, 'n_words': 328}