jimpil / duratom

A durable atom type for Clojure
Eclipse Public License 1.0
213 stars 14 forks source link

Lazy load initial-value #14

Closed abeluck closed 5 years ago

abeluck commented 5 years ago

This is a feature request (or proposal for one).

I've a use case which fits duratom well: caching and operating on a value in memory with the benefit of durability in the event of process restart.

In my case the initial-value is very expensive to calculate, so I want to avoid calculating it unless it is necessary. I can achieve this now with passing nil as the value of :init, then wrapping the duratom in a check to populate the value if necessary.

I thought it would be a nice addition to the library however if :init was optionally a function that would be invoked by duratom to create the initial value.

jimpil commented 5 years ago

Hi there,

First of all, let me make an attempt to rephrase your requirement, in order to make sure we're both on the same page:

When initialising a duratom, you want to avoid calculating the initial value (because it's expensive), if that value has already been persisted (by some previous run), in which case it can be just read in (as opposed to calculated). Am i understanding this correctly?

If yes, then this is dead easy, and actually a pretty neat feature ;). In fact, i would make both functions and delays acceptable as init values.

Would that suit your use-case (i.e. init fn/delay will be called only when no pre-persisted init is found)?

abeluck commented 5 years ago

When initialising a duratom, you want to avoid calculating the initial value (because it's expensive), if that value has already been persisted (by some previous run), in which case it can be just read in (as opposed to calculated). Am i understanding this correctly?

Exactly.

Would that suit your use-case (i.e. init fn/delay will be called only when no pre-persisted init is found)?

Indeed it would! And also supporting a delay is a nice touch.

jimpil commented 5 years ago

Perfect! I have this already committed so I'll just cut version 0.4.4 shortly :)

Thanks again