Skip to content

Commit 65e6cea

Browse files
committed
CMR-11116: pr nitpicks
1 parent f9cd940 commit 65e6cea

File tree

5 files changed

+63
-8
lines changed

5 files changed

+63
-8
lines changed

common-lib/src/cmr/common/cache/cache_spec.clj

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,12 +95,37 @@
9595
(is (= 2 (cache/get-value cache :foo always-fail)))
9696
(is (= 2 @counter))))
9797

98+
(defn- evict-test
99+
"Checks that evicting a key removes it from the cache"
100+
[cache]
101+
(let [lookup-called (atom false)
102+
lookup-fn #(do (reset! lookup-called true) "new-value")]
103+
;; Set a value
104+
(cache/set-value cache :foo "initial-value")
105+
(is (= "initial-value" (cache/get-value cache :foo)))
106+
107+
;; Evict the key
108+
(cache/evict cache :foo)
109+
110+
;; Value should be gone, lookup function should be called
111+
(is (= "new-value" (cache/get-value cache :foo lookup-fn)))
112+
(is @lookup-called "Lookup function should have been called after evict"))
113+
114+
;; Test that evict only removes the specified key
115+
(cache/reset cache)
116+
(cache/set-value cache :foo "foo-value")
117+
(cache/set-value cache :bar "bar-value")
118+
(cache/evict cache :foo)
119+
(is (nil? (cache/get-value cache :foo)))
120+
(is (= "bar-value" (cache/get-value cache :bar))))
121+
98122
(def ^:private cache-test-fns
99123
"Defines the minimum set of test functions that check a cache implementation"
100124
[#'value-type-support
101125
#'key-type-support
102126
#'get-value-with-lookup-fn-test
103-
#'clear-cache-test])
127+
#'clear-cache-test
128+
#'evict-test])
104129

105130
(defn- get-cache-test-fns
106131
"Returns the functions to test for verifying the cache. Not all caches will delete all values

redis-utils-lib/test/cmr/redis_utils/test/test_redis_cache.clj

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,31 @@
4747
(is (= -1
4848
(wcar* s-key true (redis-config/redis-conn-opts) (carmine/ttl s-key)))))))
4949

50+
(deftest test-redis-cache-evict
51+
(testing "Evict removes key from Redis cache"
52+
(let [rcache (redis-cache/create-redis-cache {:read-connection (redis-config/redis-read-conn-opts)
53+
:primary-connection (redis-config/redis-conn-opts)})
54+
lookup-called (atom false)
55+
lookup-fn (fn [] (reset! lookup-called true) "new-value")]
56+
(cache/set-value rcache "test-key" "initial-value")
57+
(is (= "initial-value" (cache/get-value rcache "test-key")))
58+
59+
(cache/evict rcache "test-key")
60+
61+
(is (= "new-value" (cache/get-value rcache "test-key" lookup-fn)))
62+
(is @lookup-called "Lookup function should have been called after evict")))
63+
64+
(testing "Evict only removes specified key"
65+
(let [rcache (redis-cache/create-redis-cache {:read-connection (redis-config/redis-read-conn-opts)
66+
:primary-connection (redis-config/redis-conn-opts)})]
67+
(cache/set-value rcache "foo" "foo-value")
68+
(cache/set-value rcache "bar" "bar-value")
69+
70+
(cache/evict rcache "foo")
71+
72+
(is (nil? (cache/get-value rcache "foo")))
73+
(is (= "bar-value" (cache/get-value rcache "bar"))))))
74+
5075
(deftest test-get-keys
5176
(wcar* "get-keys-pattern#-test1" false (redis-config/redis-conn-opts) (carmine/set "get-keys-pattern#-test1" "test1"))
5277
(wcar* "get-keys-pattern#-test2" false (redis-config/redis-conn-opts) (carmine/set "get-keys-pattern#-test2" "test2"))

system-int-test/test/cmr/system_int_test/misc/launchpad_user_cache.clj

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
(deftest launchpad-user-cache-test
2424
(testing "launchpad cache initial value"
2525
(let [launchpad-token (echo-util/login-with-launchpad-token (system/context) "user1")
26-
token-key (DigestUtils/sha256Hex launchpad-token)]
26+
token-key (keyword (DigestUtils/sha256Hex launchpad-token))]
2727
(is (empty? (cache-util/list-cache-keys (url/ingest-read-caches-url) "launchpad-user" transmit-config/mock-echo-system-token)))
2828
(let [concept (data-umm-c/collection-concept {})
2929
{:keys [concept-id revision-id]} (ingest/ingest-concept concept {:token launchpad-token})]
@@ -35,7 +35,7 @@
3535
(is (= (:uid (cache-util/get-cache-value
3636
(url/ingest-read-caches-url)
3737
"launchpad-user"
38-
token-key
38+
(name token-key)
3939
transmit-config/mock-echo-system-token
4040
200))
4141
"user1"))
@@ -52,7 +52,7 @@
5252
(let [expiration-time (:expiration-time (cache-util/get-cache-value
5353
(url/ingest-read-caches-url)
5454
"launchpad-user"
55-
token-key
55+
(name token-key)
5656
transmit-config/mock-echo-system-token
5757
200))]
5858
(ingest/ingest-concept concept {:token launchpad-token})
@@ -61,7 +61,7 @@
6161
(is (= expiration-time (:expiration-time (cache-util/get-cache-value
6262
(url/ingest-read-caches-url)
6363
"launchpad-user"
64-
token-key
64+
(name token-key)
6565
transmit-config/mock-echo-system-token
6666
200))))
6767
(testing "token cache value expires"
@@ -100,7 +100,7 @@
100100
(deftest non-transient-errors-are-cached-test
101101
(testing "Non-transient errors are cached for 5 minutes"
102102
(let [token "ABC-INV-ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ"
103-
token-key (DigestUtils/sha256Hex token)]
103+
token-key (keyword (DigestUtils/sha256Hex token))]
104104
(let [resp (ingest/ingest-concept (data-umm-c/collection-concept {}) {:token token})]
105105
(is (= 401 (:status resp))))
106106

@@ -109,7 +109,7 @@
109109
(let [cached-value (cache-util/get-cache-value
110110
(url/ingest-read-caches-url)
111111
"launchpad-user"
112-
token-key
112+
(name token-key)
113113
transmit-config/mock-echo-system-token
114114
200)]
115115
(is (false? (:valid cached-value)))

transmit-lib/src/cmr/transmit/cache/consistent_cache.clj

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,11 @@
119119
(c/set-value memory-cache key value)
120120
(c/set-value hash-cache (key->hash-cache-key key) (hash value)))
121121

122+
(evict
123+
[_this key]
124+
(c/evict memory-cache key)
125+
(c/evict hash-cache (key->hash-cache-key key)))
126+
122127
(cache-size
123128
[_]
124129
(+ (c/cache-size memory-cache)

transmit-lib/src/cmr/transmit/launchpad_user_cache.clj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
:error-type error-type
5656
:expiration-time (t/plus (time-keeper/now) (t/minutes (transmit-config/invalid-token-timeout)))})))))]
5757
(if-let [cache (cache/context->cache context launchpad-user-cache-key)]
58-
(let [cache-key (DigestUtils/sha256Hex token)
58+
(let [cache-key (keyword (DigestUtils/sha256Hex token))
5959
token-info (cache/get-value cache cache-key get-launchpad-user-fn)]
6060
(if (t/before? (:expiration-time token-info) (time-keeper/now))
6161
;; Cache entry expired (after 5 min for errors, or token lifetime for valid tokens)

0 commit comments

Comments
 (0)