@@ -19,10 +19,15 @@ public class EVCacheKey {
1919 // like max.hash.length. So changing max.hash.length alone would not necessarily trigger hash recalculation, but
2020 // one would have to change the hashing algorithm in order to having hashing properties taken into account.
2121 // This is to make such a hashing property change very obvious and not subtle.
22- private final Map <HashingAlgorithm , String > hashedKeysByAlgorithm ;
23- private final Map <HashingAlgorithm , String > hashedKeysByAlgorithmForDuet ;
22+ private final Map <String , String > hashedKeysByAlgorithm ;
23+ private final Map <String , String > hashedKeysByAlgorithmForDuet ;
24+ private final String encoder ;
2425
2526 public EVCacheKey (String appName , String key , String canonicalKey , HashingAlgorithm hashingAlgorithmAtAppLevel , Property <Boolean > shouldEncodeHashKeyAtAppLevel , Property <Integer > maxDigestBytesAtAppLevel , Property <Integer > maxHashLengthAtAppLevel ) {
27+ this (appName , key , canonicalKey , hashingAlgorithmAtAppLevel , shouldEncodeHashKeyAtAppLevel , maxDigestBytesAtAppLevel , maxHashLengthAtAppLevel , null );
28+ }
29+
30+ public EVCacheKey (String appName , String key , String canonicalKey , HashingAlgorithm hashingAlgorithmAtAppLevel , Property <Boolean > shouldEncodeHashKeyAtAppLevel , Property <Integer > maxDigestBytesAtAppLevel , Property <Integer > maxHashLengthAtAppLevel , String encoder ) {
2631 super ();
2732 this .appName = appName ;
2833 this .key = key ;
@@ -31,6 +36,7 @@ public EVCacheKey(String appName, String key, String canonicalKey, HashingAlgori
3136 this .shouldEncodeHashKeyAtAppLevel = shouldEncodeHashKeyAtAppLevel ;
3237 this .maxDigestBytesAtAppLevel = maxDigestBytesAtAppLevel ;
3338 this .maxHashLengthAtAppLevel = maxHashLengthAtAppLevel ;
39+ this .encoder = encoder ;
3440 hashedKeysByAlgorithm = new HashMap <>();
3541 hashedKeysByAlgorithmForDuet = new HashMap <>();
3642 }
@@ -59,11 +65,11 @@ private String getCanonicalKeyForDuet() {
5965
6066 @ Deprecated
6167 public String getHashKey () {
62- return getHashKey (hashingAlgorithmAtAppLevel , null == shouldEncodeHashKeyAtAppLevel ? null : shouldEncodeHashKeyAtAppLevel .get (), null == maxDigestBytesAtAppLevel ? null : maxDigestBytesAtAppLevel .get (), null == maxHashLengthAtAppLevel ? null : maxHashLengthAtAppLevel .get ());
68+ return getHashKey (hashingAlgorithmAtAppLevel , null == shouldEncodeHashKeyAtAppLevel ? null : shouldEncodeHashKeyAtAppLevel .get (), null == maxDigestBytesAtAppLevel ? null : maxDigestBytesAtAppLevel .get (), null == maxHashLengthAtAppLevel ? null : maxHashLengthAtAppLevel .get (), encoder );
6369 }
6470
6571 // overlays app level hashing and client level hashing
66- public String getHashKey (boolean isDuet , HashingAlgorithm hashingAlgorithm , Boolean shouldEncodeHashKey , Integer maxDigestBytes , Integer maxHashLength ) {
72+ public String getHashKey (boolean isDuet , HashingAlgorithm hashingAlgorithm , Boolean shouldEncodeHashKey , Integer maxDigestBytes , Integer maxHashLength , String baseEnoder ) {
6773 if (hashingAlgorithm == HashingAlgorithm .NO_HASHING ) {
6874 return null ;
6975 }
@@ -83,36 +89,52 @@ public String getHashKey(boolean isDuet, HashingAlgorithm hashingAlgorithm, Bool
8389 if (null == maxHashLength ) {
8490 maxHashLength = this .maxHashLengthAtAppLevel .get ();
8591 }
92+
93+ if (null == baseEnoder ) {
94+ baseEnoder = encoder ;
95+ }
8696
87- return isDuet ? getHashKeyForDuet (hashingAlgorithm , shouldEncodeHashKey , maxDigestBytes , maxHashLength ) : getHashKey (hashingAlgorithm , shouldEncodeHashKey , maxDigestBytes , maxHashLength );
97+ return isDuet ? getHashKeyForDuet (hashingAlgorithm , shouldEncodeHashKey , maxDigestBytes , maxHashLength , baseEnoder ) : getHashKey (hashingAlgorithm , shouldEncodeHashKey , maxDigestBytes , maxHashLength , baseEnoder );
8898 }
8999
90100 // overlays app level hashing algorithm and client level hashing algorithm
91- public String getDerivedKey (boolean isDuet , HashingAlgorithm hashingAlgorithm , Boolean shouldEncodeHashKey , Integer maxDigestBytes , Integer maxHashLength ) {
101+ public String getDerivedKey (boolean isDuet , HashingAlgorithm hashingAlgorithm , Boolean shouldEncodeHashKey , Integer maxDigestBytes , Integer maxHashLength , String baseEnoder ) {
92102 // this overlay of hashingAlgorithm helps determine if there at all needs to be hashing performed, otherwise, will return canonical key
93103 if (null == hashingAlgorithm ) {
94104 hashingAlgorithm = hashingAlgorithmAtAppLevel ;
95105 }
96106
97- return null == hashingAlgorithm || hashingAlgorithm == HashingAlgorithm .NO_HASHING ? getCanonicalKey (isDuet ) : getHashKey (isDuet , hashingAlgorithm , shouldEncodeHashKey , maxDigestBytes , maxHashLength );
107+ return null == hashingAlgorithm || hashingAlgorithm == HashingAlgorithm .NO_HASHING ? getCanonicalKey (isDuet ) : getHashKey (isDuet , hashingAlgorithm , shouldEncodeHashKey , maxDigestBytes , maxHashLength , baseEnoder );
98108 }
99109
100- private String getHashKey (HashingAlgorithm hashingAlgorithm , Boolean shouldEncodeHashKey , Integer maxDigestBytes , Integer maxHashLength ) {
110+ private String getHashKey (HashingAlgorithm hashingAlgorithm , Boolean shouldEncodeHashKey , Integer maxDigestBytes , Integer maxHashLength , String encoder ) {
101111 if (null == hashingAlgorithm ) {
102112 return null ;
103113 }
104114
115+ final String key = hashingAlgorithm .toString ()+ maxDigestBytes != null ? maxDigestBytes .toString () : "-" + maxHashLength != null ? maxHashLength .toString () : "-" + encoder != null ? encoder : "-" ;
116+ String val = hashedKeysByAlgorithm .get (key );
117+ if (val == null ) {
118+ val = KeyHasher .getHashedKeyEncoded (getCanonicalKeyForDuet (), hashingAlgorithm , maxDigestBytes , maxHashLength , encoder );
119+ hashedKeysByAlgorithm .put (key , val );
120+ }
105121 // TODO: Once the issue around passing hashedKey in bytes[] is figured, we will start using (nullable) shouldEncodeHashKey, and call KeyHasher.getHashedKeyInBytes() accordingly
106- return hashedKeysByAlgorithm . computeIfAbsent ( hashingAlgorithm , ha -> KeyHasher . getHashedKeyEncoded ( canonicalKey , ha , maxDigestBytes , maxHashLength )) ;
122+ return val ;
107123 }
108124
109- private String getHashKeyForDuet (HashingAlgorithm hashingAlgorithm , Boolean shouldEncodeHashKey , Integer maxDigestBytes , Integer maxHashLength ) {
125+ private String getHashKeyForDuet (HashingAlgorithm hashingAlgorithm , Boolean shouldEncodeHashKey , Integer maxDigestBytes , Integer maxHashLength , String encoder ) {
110126 if (null == hashingAlgorithm ) {
111127 return null ;
112128 }
113129
130+ final String key = hashingAlgorithm .toString ()+ maxDigestBytes != null ? maxDigestBytes .toString () : "-" + maxHashLength != null ? maxHashLength .toString () : "-" + encoder != null ? encoder : "-" ;
131+ String val = hashedKeysByAlgorithmForDuet .get (key );
132+ if (val == null ) {
133+ val = KeyHasher .getHashedKeyEncoded (getCanonicalKeyForDuet (), hashingAlgorithm , maxDigestBytes , maxHashLength , encoder );
134+ hashedKeysByAlgorithmForDuet .put (key , val );
135+ }
114136 // TODO: Once the issue around passing hashedKey in bytes[] is figured, we will start using (nullable) shouldEncodeHashKey, and call KeyHasher.getHashedKeyInBytes() accordingly
115- return hashedKeysByAlgorithmForDuet . computeIfAbsent ( hashingAlgorithm , ha -> KeyHasher . getHashedKeyEncoded ( getCanonicalKeyForDuet (), ha , maxDigestBytes , maxHashLength )) ;
137+ return val ;
116138 }
117139
118140 @ Override
0 commit comments