1
- package redis .clients .jedis .csc . util ;
1
+ package redis .clients .jedis .csc ;
2
2
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
- import redis .clients .jedis .csc .ClientSideCache ;
11
- import redis .clients .jedis .csc .ClientSideCacheable ;
12
- import redis .clients .jedis .csc .DefaultClientSideCacheable ;
8
+ import redis .clients .jedis .csc .hash .CommandLongHashing ;
9
+ import redis .clients .jedis .csc .hash .GuavaHashing ;
13
10
14
11
public class GuavaCSC extends ClientSideCache {
15
12
16
- private static final HashFunction DEFAULT_HASH_FUNCTION = com .google .common .hash .Hashing .fingerprint2011 ();
17
-
18
13
private final Cache <Long , Object > cache ;
19
- private final HashFunction hashFunction ;
20
14
21
15
public GuavaCSC (Cache <Long , Object > guavaCache ) {
22
- this (guavaCache , DEFAULT_HASH_FUNCTION );
16
+ this (guavaCache , GuavaHashing . DEFAULT_HASH_FUNCTION );
23
17
}
24
18
25
19
public GuavaCSC (Cache <Long , Object > guavaCache , HashFunction hashFunction ) {
26
- super ();
20
+ this (guavaCache , new GuavaHashing (hashFunction ));
21
+ }
22
+
23
+ public GuavaCSC (Cache <Long , Object > guavaCache , CommandLongHashing hashing ) {
24
+ super (hashing );
27
25
this .cache = guavaCache ;
28
- this .hashFunction = hashFunction ;
29
26
}
30
27
31
28
public GuavaCSC (Cache <Long , Object > guavaCache , ClientSideCacheable cacheable ) {
32
- this (guavaCache , DEFAULT_HASH_FUNCTION , cacheable );
29
+ this (guavaCache , new GuavaHashing ( GuavaHashing . DEFAULT_HASH_FUNCTION ) , cacheable );
33
30
}
34
31
35
- public GuavaCSC (Cache <Long , Object > cache , HashFunction function , ClientSideCacheable cacheable ) {
36
- super (cacheable );
32
+ public GuavaCSC (Cache <Long , Object > cache , CommandLongHashing hashing , ClientSideCacheable cacheable ) {
33
+ super (hashing , cacheable );
37
34
this .cache = cache ;
38
- this .hashFunction = function ;
39
35
}
40
36
41
37
@ Override
@@ -58,14 +54,6 @@ protected Object getValue(long hash) {
58
54
return cache .getIfPresent (hash );
59
55
}
60
56
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
57
public static Builder builder () {
70
58
return new Builder ();
71
59
}
@@ -76,7 +64,9 @@ public static class Builder {
76
64
private long expireTime = DEFAULT_EXPIRE_SECONDS ;
77
65
private final TimeUnit expireTimeUnit = TimeUnit .SECONDS ;
78
66
79
- private HashFunction hashFunction = DEFAULT_HASH_FUNCTION ;
67
+ // not using a default value to avoid an object creation like 'new GuavaHashing(hashFunction)'
68
+ private HashFunction hashFunction = null ;
69
+ private CommandLongHashing longHashing = null ;
80
70
81
71
private ClientSideCacheable cacheable = DefaultClientSideCacheable .INSTANCE ;
82
72
@@ -94,6 +84,13 @@ public Builder ttl(int seconds) {
94
84
95
85
public Builder hashFunction (HashFunction function ) {
96
86
this .hashFunction = function ;
87
+ this .longHashing = null ;
88
+ return this ;
89
+ }
90
+
91
+ public Builder hashing (CommandLongHashing hashing ) {
92
+ this .longHashing = hashing ;
93
+ this .hashFunction = null ;
97
94
return this ;
98
95
}
99
96
@@ -109,7 +106,9 @@ public GuavaCSC build() {
109
106
110
107
cb .expireAfterWrite (expireTime , expireTimeUnit );
111
108
112
- return new GuavaCSC (cb .build (), hashFunction , cacheable );
109
+ return longHashing != null ? new GuavaCSC (cb .build (), longHashing , cacheable )
110
+ : hashFunction != null ? new GuavaCSC (cb .build (), new GuavaHashing (hashFunction ), cacheable )
111
+ : new GuavaCSC (cb .build (), cacheable );
113
112
}
114
113
}
115
114
}
0 commit comments