Skip to content

Commit 47906a6

Browse files
committed
Tweak the caching
1 parent 1b6e8b7 commit 47906a6

File tree

4 files changed

+43
-39
lines changed

4 files changed

+43
-39
lines changed

src/main/java/org/maxgamer/quickshop/Cache.java

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,14 @@
2121

2222
import com.google.common.cache.CacheBuilder;
2323
import com.google.common.cache.CacheStats;
24+
import lombok.AllArgsConstructor;
25+
import lombok.Data;
2426
import org.bukkit.Location;
2527
import org.checkerframework.checker.nullness.qual.NonNull;
2628
import org.jetbrains.annotations.NotNull;
2729
import org.jetbrains.annotations.Nullable;
2830
import org.maxgamer.quickshop.api.shop.Shop;
31+
import org.maxgamer.quickshop.shop.SimpleShopManager;
2932

3033
import java.util.concurrent.TimeUnit;
3134

@@ -36,11 +39,10 @@
3639
*/
3740
public class Cache {
3841
private final QuickShop plugin;
39-
private final com.google.common.cache.Cache<Location, Shop> accessCaching = CacheBuilder
42+
private final com.google.common.cache.Cache<Location, BoxedShop> accessCaching = CacheBuilder
4043
.newBuilder()
41-
.initialCapacity(1000)
44+
.initialCapacity(5000)
4245
.expireAfterAccess(120, TimeUnit.MINUTES)
43-
.weakValues()
4446
.recordStats()
4547
.build();
4648

@@ -56,24 +58,22 @@ public Cache(QuickShop plugin) {
5658
/**
5759
* Gets shop from plugin caching
5860
*
59-
* @param location The shop location that you want to get
60-
* @param includeAttached Include attached shops
61+
* @param location The shop location that you want to get
62+
* @param attached Does search for attached
6163
* @return The shop, null for no shops found in caching and memory
6264
*/
6365
@Nullable
64-
public Shop getCaching(@NotNull Location location, boolean includeAttached) {
65-
Shop shop = accessCaching.getIfPresent(location);
66-
if (shop == null) {
67-
if (includeAttached) {
68-
shop = plugin.getShopManager().getShopIncludeAttached(location, false);
66+
public Shop find(@NotNull Location location, boolean attached) {
67+
BoxedShop boxedShop = accessCaching.getIfPresent(location);
68+
if (boxedShop == null) {
69+
if (attached) {
70+
boxedShop = new BoxedShop(((SimpleShopManager) plugin.getShopManager()).findShopIncludeAttached(location, false));
6971
} else {
70-
shop = plugin.getShopManager().getShop(location);
72+
boxedShop = new BoxedShop(plugin.getShopManager().getShop(location));
7173
}
7274
}
73-
if (shop != null) {
74-
setCache(location, shop);
75-
}
76-
return shop;
75+
setCache(location, boxedShop.getShop());
76+
return boxedShop.getShop();
7777
}
7878

7979
/**
@@ -83,10 +83,20 @@ public Shop getCaching(@NotNull Location location, boolean includeAttached) {
8383
* @param shop null for invalidate and Shop object for update
8484
*/
8585
public void setCache(@NotNull Location location, @Nullable Shop shop) {
86-
if (shop == null) {
87-
accessCaching.invalidate(location);
88-
return;
86+
accessCaching.put(location, new BoxedShop(shop));
87+
}
88+
89+
public void invalidate(@NotNull Location location) {
90+
accessCaching.invalidate(location);
91+
}
92+
93+
@AllArgsConstructor
94+
@Data
95+
static class BoxedShop {
96+
private Shop shop;
97+
98+
public boolean isPresent() {
99+
return shop != null;
89100
}
90-
accessCaching.put(location, shop);
91101
}
92102
}

src/main/java/org/maxgamer/quickshop/listener/AbstractProtectionListener.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* This file is a part of project QuickShop, the name is ProtectionListenerBase.java
2+
* This file is a part of project QuickShop, the name is AbstractProtectionListener.java
33
* Copyright (C) PotatoCraft Studio and contributors
44
*
55
* This program is free software: you can redistribute it and/or modify it
@@ -52,7 +52,7 @@ public QuickShop getPlugin() {
5252
@Nullable
5353
public Shop getShopRedstone(@NotNull Location location, boolean includeAttached) {
5454
if (cache != null) {
55-
return cache.getCaching(location, includeAttached);
55+
return cache.find(location, includeAttached);
5656
} else {
5757
if (includeAttached) {
5858
return plugin.getShopManager().getShopIncludeAttached(location);

src/main/java/org/maxgamer/quickshop/listener/BlockListener.java

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -169,36 +169,25 @@ private Shop getShopNextTo(@NotNull Location loc) {
169169
@EventHandler(ignoreCancelled = true, priority = EventPriority.MONITOR)
170170
public void onInventoryMove(InventoryMoveItemEvent event) {
171171
if (!this.update_sign_when_inventory_moving) {
172-
Util.debugLog("Sign update was disabled");
173172
return;
174173
}
175-
176174
Location destination = event.getDestination().getLocation();
177175
Location source = event.getSource().getLocation();
178176
Shop destShop = null;
179177
Shop sourceShop = null;
180178
if (destination != null) {
181179
destination = Util.getBlockLocation(destination);
182-
Util.debugLog("Destination found: " + destination);
183-
destShop = getShopPlayer(destination, true);
180+
destShop = getShopRedstone(destination, true);
184181
}
185182
if (source != null) {
186183
source = Util.getBlockLocation(source);
187-
Util.debugLog("Source found: " + destination);
188-
sourceShop = getShopPlayer(source, true);
184+
sourceShop = getShopRedstone(source, true);
189185
}
190-
191186
if (destShop != null) {
192-
Util.debugLog("Destination shop found: " + destShop);
193187
super.getPlugin().getSignUpdateWatcher().scheduleSignUpdate(destShop);
194-
} else {
195-
Util.debugLog("Destination shop not found.");
196188
}
197189
if (sourceShop != null) {
198-
Util.debugLog("Source shop found: " + sourceShop);
199190
super.getPlugin().getSignUpdateWatcher().scheduleSignUpdate(sourceShop);
200-
} else {
201-
Util.debugLog("Source shop not found.");
202191
}
203192
}
204193

src/main/java/org/maxgamer/quickshop/shop/SimpleShopManager.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,12 @@ public void createShop(@NotNull Shop shop, @NotNull Info info) {
417417
Util.debugLog("Location is null.");
418418
return null;
419419
}
420-
return getShopIncludeAttached_Fast(loc, false, useCache);
420+
if (useCache) {
421+
if (plugin.getShopCache() != null) {
422+
return plugin.getShopCache().find(loc, true);
423+
}
424+
}
425+
return findShopIncludeAttached(loc, false);
421426
}
422427

423428
@Override
@@ -1439,8 +1444,8 @@ private void actionTrade(@NotNull Player p, Info info, @NotNull String message)
14391444
}
14401445
}
14411446

1442-
private @Nullable Shop getShopIncludeAttached_Fast(
1443-
@NotNull Location loc, boolean fromAttach, boolean writeCache) {
1447+
@Nullable
1448+
public Shop findShopIncludeAttached(@NotNull Location loc, boolean fromAttach) {
14441449
Shop shop = getShop(loc);
14451450

14461451
// failed, get attached shop
@@ -1455,7 +1460,7 @@ private void actionTrade(@NotNull Player p, Info info, @NotNull String message)
14551460
if (Util.isWallSign(currentBlock.getType())) {
14561461
final Block attached = Util.getAttached(currentBlock);
14571462
if (attached != null) {
1458-
shop = this.getShopIncludeAttached_Fast(attached.getLocation(), true, writeCache);
1463+
shop = this.findShopIncludeAttached(attached.getLocation(), true);
14591464
}
14601465
} else {
14611466
// optimize for performance
@@ -1471,7 +1476,7 @@ private void actionTrade(@NotNull Player p, Info info, @NotNull String message)
14711476
}
14721477
}
14731478
// add cache if using
1474-
if (plugin.getShopCache() != null && writeCache) {
1479+
if (plugin.getShopCache() != null) {
14751480
plugin.getShopCache().setCache(loc, shop);
14761481
}
14771482
return shop;

0 commit comments

Comments
 (0)