Skip to content

Commit

Permalink
fix: zset lru cache (#372)
Browse files Browse the repository at this point in the history
  • Loading branch information
caojiajun committed Jan 15, 2025
1 parent 66a8385 commit 79f162a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@ public class RedisZSet implements EstimateSizeValue {

private static final Comparator<BytesKey> lexComparator = (o1, o2) -> BytesUtils.compare(o1.getKey(), o2.getKey());
private static final Comparator<ZSetTuple> scoreComparator = (o1, o2) -> {
if (o1.getMember().equals(o2.getMember())) {
return 0;
}
int compare = Double.compare(o1.getScore(), o2.getScore());
if (compare != 0) {
return compare;
Expand Down Expand Up @@ -55,8 +52,10 @@ public Map<BytesKey, Double> zadd(Map<BytesKey, Double> map) {
estimateSize += 8;
estimateSize += entry.getKey().getKey().length;
}
if (put != null) {
scoreSet.remove(new ZSetTuple(entry.getKey(), put));
}
ZSetTuple zSetTuple = new ZSetTuple(entry.getKey(), entry.getValue());
scoreSet.remove(zSetTuple);
scoreSet.add(zSetTuple);
}
return existsMap;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ public class RedisZSetLRUTest {

@Test
public void test() {
System.out.println("a=" + Utils.bytesToString(a.getKey()));
System.out.println("b=" + Utils.bytesToString(b.getKey()));
System.out.println("c=" + Utils.bytesToString(c.getKey()));
System.out.println("d=" + Utils.bytesToString(d.getKey()));
System.out.println("e=" + Utils.bytesToString(e.getKey()));
System.out.println("f=" + Utils.bytesToString(f.getKey()));

Map<BytesKey, Double> map = new HashMap<>();
map.put(a, 1.0);
map.put(b, 2.0);
Expand Down Expand Up @@ -91,7 +98,9 @@ public void test() {
assertEquals(zrange.get(2).getScore(), 5.0, 0.00001);

List<ZSetTuple> zrange1 = redisZSet.zrange(2, 10);

assertEquals(zrange1.size(), 4);

assertEquals(zrange1.get(0).getMember(), c);
assertEquals(zrange1.get(0).getScore(), 3.0, 0.00001);
assertEquals(zrange1.get(1).getMember(), d);
Expand Down

0 comments on commit 79f162a

Please sign in to comment.