ClojureDocs

Nav

Namespaces

ref

clojure.core

Available since 1.0 (source)
  • (ref x)
  • (ref x & options)
Creates and returns a Ref with an initial value of x and zero or
more options (in any order):
 :meta metadata-map
 :validator validate-fn
 :min-history (default 0)
:max-history (default 10)
 If metadata-map is supplied, it will become the metadata on the
ref. validate-fn must be nil or a side-effect-free fn of one
argument, which will be passed the intended new state on any state
change. If the new state is unacceptable, the validate-fn should
return false or throw an exception. validate-fn will be called on
transaction commit, when all refs have their final values.
 Normally refs accumulate history dynamically as needed to deal with
read demands. If you know in advance you will need history you can
set :min-history to ensure it will be available when first needed (instead
of after a read fault). History is limited, and the limit can be set
with :max-history.
4 Examples
user=> (ref [])
#<ref@5fafa486: []>

user=> (ref 1 :validator pos?)
#<Ref@6c484c6b: 1>

=> (ref 0 :validator pos?)
IllegalStateException Invalid reference state  clojure.lang.ARef.validate (ARef.java:33)

=> (dosync (ref-set (ref 1 :validator pos?) 0))
IllegalStateException Invalid reference state  clojure.lang.ARef.validate (ARef.java:33)

=> (dosync (ref-set (ref 1 :validator pos?) 2))
2
; create(ref)
(def a (ref '(1 2 3)))

; read(deref)
(deref a) ; -> (1 2 3)

; rewrite(ref-set)
; (ref-set a '(3 2 1)) err!
(dosync (ref-set a '(3 2 1)))

(deref a) ; -> (3 2 1)
;; EXAMPLE: Storing an object in a state map to control it's lifecycle
(def state (ref {}))

(defn start->streams
  []
  (log/info "[start->streams] enter")

;; deref state and do something with it (in this case, check if we have created it)
  (when (not (instance? KafkaStreams (:streams @state)))
    (let [config-file (s/conform ::configs/configuration-file (configs/config CONFIG_PATH))
          kafka-config            (s/conform ::configs/kafka-configuration (:processor.config/kafka-configuration config-file))
          stream-processing-props {StreamsConfig/APPLICATION_ID_CONFIG            (:applicationid kafka-config)
                                   StreamsConfig/COMMIT_INTERVAL_MS_CONFIG        (:auto.commit.interval.ms kafka-config)
                                   StreamsConfig/BOOTSTRAP_SERVERS_CONFIG         (:bootstrap-servers kafka-config)
                                   StreamsConfig/DEFAULT_KEY_SERDE_CLASS_CONFIG   (.getName (.getClass (Serdes/String)))
                                   StreamsConfig/DEFAULT_VALUE_SERDE_CLASS_CONFIG (.getName (.getClass (Serdes/String)))
                                   StreamsConfig/PROCESSING_GUARANTEE_CONFIG      StreamsConfig/EXACTLY_ONCE}]
      (try 
        (log/infof "[start->streams] creating kafka stream with config: %s" stream-processing-props)

        (dosync 

;; update the ref and store an object in the state map
         (alter state conj (-> { :streams (KafkaStreams. (topology) (StreamsConfig. stream-processing-props))})))
         (log/info "[start->streams] stream created")

         (catch Exception e (log/error e)))))

;; deref the state and call an fn on the contained object
  (.start (:streams @state)))
;; Refs automatically deref their content when used as a function:
(def m (ref {:a 1 :b 2}))

(m :a)
;; 1

;; Refs natural ordering is by creation time (oldest first), not their content:
(def z (ref 10))
(def y (ref 12))
(def x (ref 1))

(map deref (sort [x y z]))
;; (10 12 1)
See Also

Must be called in a transaction. Sets the in-transaction-value of ref to: (apply fun in-transac...

Added by zk

Must be called in a transaction. Sets the value of ref. Returns val.

Added by zk

Adds a watch function to an agent/atom/var/ref reference. The watch fn must be a fn of 4 args: a k...

Added by Claj

Runs the exprs (in an implicit do) in a transaction that encompasses exprs and any nested calls. ...

Added by kumarshantanu

Must be called in a transaction. Sets the in-transaction-value of ref to: (apply fun in-transac...

Added by eric

Must be called in a transaction. Protects the ref from modification by other transactions. Return...

Added by eric

Returns the history count of a ref

Added by kumarshantanu

Gets the min-history of a ref, or sets it and returns the ref

Added by kumarshantanu

Gets the max-history of a ref, or sets it and returns the ref

Added by kumarshantanu

Sets the validator-fn for a var/ref/agent/atom. validator-fn must be nil or a side-effect-free fn ...

Added by kumarshantanu
0 Notes
No notes for ref