<commit_before>(ns demo.caching
  (:require [immutant.caching :as c]
            [immutant.caching.core-memoize :refer [memo]]))

;;; Caches implement org.infinispan.Cache and
;;; java.util.concurrent.ConcurrentMap. They are mutable maps, so
;;; changing them requires Java interop, as would any mutable Java
;;; data structure. Of course, reading entries can be done with core
;;; Clojure functions.

;;; Writing

(def foo (c/cache "foo", :ttl [5 :minutes], :idle [1 :minute]))

;;; Put an entry in the cache
(.put foo :a 1)

;;; Override its time-to-live
;;; TODO
;;; (cache/put foo :a 1 {:ttl [1 :hour]})

;;; Add all the entries in the map to the cache
(.putAll foo {:b 2, :c 3})

;;; Put it in only if key is not already present
(.putIfAbsent foo :b 6)                 ;=> 2
(.putIfAbsent foo :d 4)                 ;=> nil

;;; Put it in only if key is already present
(.replace foo :e 5)                     ;=> nil
(.replace foo :b 6)                     ;=> 2

;;; Put it in only if key is there and current matches old
(.replace foo :b 2 0)                   ;=> false
(.replace foo :b 6 0)                   ;=> true
(:b foo)                                ;=> 0


;;; Reading
<commit_msg>Override ttl for an entry<commit_after>(ns demo.caching
  (:require [immutant.caching :as c]
            [immutant.caching.core-memoize :refer [memo]])
  (:import java.util.concurrent.TimeUnit))

;;; Caches implement org.infinispan.Cache and
;;; java.util.concurrent.ConcurrentMap. They are mutable maps, so
;;; changing them requires Java interop, as would any mutable Java
;;; data structure. Of course, reading entries can be done with core
;;; Clojure functions.

;;; Writing

(def foo (c/cache "foo", :ttl [5 :minutes], :idle [1 :minute]))

;;; Put an entry in the cache
(.put foo :a 1)

;;; Override its time-to-live
(.put foo :a 1, 1 TimeUnit/HOURS)

;;; Add all the entries in the map to the cache
(.putAll foo {:b 2, :c 3})

;;; Put it in only if key is not already present
(.putIfAbsent foo :b 6)                 ;=> 2
(.putIfAbsent foo :d 4)                 ;=> nil

;;; Put it in only if key is already present
(.replace foo :e 5)                     ;=> nil
(.replace foo :b 6)                     ;=> 2

;;; Put it in only if key is there and current matches old
(.replace foo :b 2 0)                   ;=> false
(.replace foo :b 6 0)                   ;=> true
(:b foo)                                ;=> 0


;;; Reading

{'fraction_non_alphanumeric': 0.1347022587268994, 'fraction_numerical': 0.014784394250513347, 'mean_word_length': 2.2697986577181206, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3786754', 'n_tokens_mistral': 836, 'n_tokens_neox': 767, 'n_words': 318}