Skip to content

Commit

Permalink
Cache replacement
Browse files Browse the repository at this point in the history
  • Loading branch information
gthea committed Dec 11, 2023
1 parent 2f664d3 commit aa125c4
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.split.android.client.service;
package tests.service;

import static org.junit.Assert.assertNull;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,17 @@
package io.split.android.client.service.impressions;

import androidx.annotation.Nullable;
import android.util.LruCache;

import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import androidx.annotation.Nullable;

import io.split.android.client.impressions.Impression;

public class ImpressionsObserver {

private final Cache<Long, Long> mCache;
private final LruCache<Long, Long> mCache;

public ImpressionsObserver(long size) {
mCache = CacheBuilder.newBuilder()
.maximumSize(size)
.concurrencyLevel(4) // Just setting the default value explicitly
.build();
mCache = new LruCache<Long, Long>((size > Integer.MAX_VALUE) ? Integer.MAX_VALUE : (int) size);
}

@Nullable
Expand All @@ -25,8 +21,10 @@ public Long testAndSet(Impression impression) {
}

Long hash = ImpressionHasher.process(impression);
Long previous = mCache.getIfPresent(hash);
@Nullable
Long previous = mCache.get(hash);
mCache.put(hash, impression.time());

return (previous == null ? null : Math.min(previous, impression.time()));
}
}

0 comments on commit aa125c4

Please sign in to comment.