3
3
import com .google .common .cache .Cache ;
4
4
import com .google .common .cache .CacheBuilder ;
5
5
import com .google .common .hash .HashFunction ;
6
- import com .google .common .hash .Hasher ;
7
6
import java .util .concurrent .TimeUnit ;
8
7
9
- import redis .clients .jedis .CommandObject ;
10
8
import redis .clients .jedis .csc .ClientSideCache ;
11
9
import redis .clients .jedis .csc .ClientSideCacheable ;
12
10
import redis .clients .jedis .csc .DefaultClientSideCacheable ;
11
+ import redis .clients .jedis .csc .hash .CommandLongHashing ;
12
+ import redis .clients .jedis .csc .hash .GuavaHashing ;
13
13
14
14
public class GuavaCSC extends ClientSideCache {
15
15
16
- private static final HashFunction DEFAULT_HASH_FUNCTION = com .google .common .hash .Hashing .fingerprint2011 ();
17
-
18
16
private final Cache <Long , Object > cache ;
19
- private final HashFunction hashFunction ;
20
17
21
18
public GuavaCSC (Cache <Long , Object > guavaCache ) {
22
- this (guavaCache , DEFAULT_HASH_FUNCTION );
19
+ this (guavaCache , GuavaHashing . DEFAULT_HASH_FUNCTION );
23
20
}
24
21
25
22
public GuavaCSC (Cache <Long , Object > guavaCache , HashFunction hashFunction ) {
26
- super ();
23
+ this (guavaCache , new GuavaHashing (hashFunction ));
24
+ }
25
+
26
+ public GuavaCSC (Cache <Long , Object > guavaCache , CommandLongHashing hashing ) {
27
+ super (hashing );
27
28
this .cache = guavaCache ;
28
- this .hashFunction = hashFunction ;
29
29
}
30
30
31
31
public GuavaCSC (Cache <Long , Object > guavaCache , ClientSideCacheable cacheable ) {
32
- this (guavaCache , DEFAULT_HASH_FUNCTION , cacheable );
32
+ this (guavaCache , new GuavaHashing ( GuavaHashing . DEFAULT_HASH_FUNCTION ) , cacheable );
33
33
}
34
34
35
- public GuavaCSC (Cache <Long , Object > cache , HashFunction function , ClientSideCacheable cacheable ) {
36
- super (cacheable );
35
+ public GuavaCSC (Cache <Long , Object > cache , CommandLongHashing hashing , ClientSideCacheable cacheable ) {
36
+ super (hashing , cacheable );
37
37
this .cache = cache ;
38
- this .hashFunction = function ;
39
38
}
40
39
41
40
@ Override
@@ -58,14 +57,6 @@ protected Object getValue(long hash) {
58
57
return cache .getIfPresent (hash );
59
58
}
60
59
61
- @ Override
62
- protected final long getHash (CommandObject command ) {
63
- Hasher hasher = hashFunction .newHasher ();
64
- command .getArguments ().forEach (raw -> hasher .putBytes (raw .getRaw ()));
65
- hasher .putInt (command .getBuilder ().hashCode ());
66
- return hasher .hash ().asLong ();
67
- }
68
-
69
60
public static Builder builder () {
70
61
return new Builder ();
71
62
}
@@ -76,7 +67,9 @@ public static class Builder {
76
67
private long expireTime = DEFAULT_EXPIRE_SECONDS ;
77
68
private final TimeUnit expireTimeUnit = TimeUnit .SECONDS ;
78
69
79
- private HashFunction hashFunction = DEFAULT_HASH_FUNCTION ;
70
+ // not using a default value to avoid an object creation like 'new GuavaHashing(hashFunction)'
71
+ private HashFunction hashFunction = null ;
72
+ private CommandLongHashing longHashing = null ;
80
73
81
74
private ClientSideCacheable cacheable = DefaultClientSideCacheable .INSTANCE ;
82
75
@@ -94,6 +87,13 @@ public Builder ttl(int seconds) {
94
87
95
88
public Builder hashFunction (HashFunction function ) {
96
89
this .hashFunction = function ;
90
+ this .longHashing = null ;
91
+ return this ;
92
+ }
93
+
94
+ public Builder hashing (CommandLongHashing hashing ) {
95
+ this .longHashing = hashing ;
96
+ this .hashFunction = null ;
97
97
return this ;
98
98
}
99
99
@@ -109,7 +109,9 @@ public GuavaCSC build() {
109
109
110
110
cb .expireAfterWrite (expireTime , expireTimeUnit );
111
111
112
- return new GuavaCSC (cb .build (), hashFunction , cacheable );
112
+ return longHashing != null ? new GuavaCSC (cb .build (), longHashing )
113
+ : hashFunction != null ? new GuavaCSC (cb .build (), hashFunction )
114
+ : new GuavaCSC (cb .build ());
113
115
}
114
116
}
115
117
}
0 commit comments