Fork me on GitHub
#ring
<
2022-05-17
>
Thierry09:05:29

Hi everyone! I have a quick question. Is it possible to access the contents of ring.middleware.session.memory/memory-store without passing a predefined atom? I know I can access the data if I define an atom and pass that as argument to memory-store. If I don't then the function creates an unnamed atom, how do I access the unnamed atom? It only returns the following without me being able to access it. #object [ring.middleware.session.memory.MemoryStore 0x1295a8d0 "ring.middleware.session.memory.MemoryStore@1295a8d0"]

jumar09:05:56

do you want that for debugging or what do you want to do with that?

Thierry09:05:39

What I want to do in the end is get the ips that are logged in and the session count per ip.

jumar09:05:02

Is that information going to be available in your session? I feel like you should use a separate mechanism for getting these stats. But you could use something like this as a workaround:

(comment
  (def ms (memory-store))
  (.write-session ms "k" "val")
  (.read-session ms "k")
  (.write-session ms "k2" "val2")

  (.session_map ms)
  ;; => #atom[{"k" "val", "k2" "val2"} 0x7f51bf9]

  .)

1
Thierry09:05:03

Thanks! the (.session_map X) was exactly what I was looking for

Thierry09:05:13

no idea why i couldnt find that with google

jumar09:05:34

Of course, this relies on an implementation detail 🙂 I just used cider-inspect in emacs to look at the contents of ms var.

Thierry09:05:55

I use Calva in VSCode, no clue how to do cider-inspect there

jumar10:05:46

Maybe it's not as easy. This one is less convenient but I used to use it a while ago:

(->> (clojure.reflect/reflect ms) :members (sort-by :name) (clojure.pprint/print-table [:name :flags :parameter-types :return-type]))

Thierry10:05:05

That works, thanks! Looking at the output I was close, but not close enough haha

Thierry10:05:44

(vec (.getMethods (.getClass session-store))) found some of them, but didnt include session_map