# Caching data per call with command pattern

**URL:** <https://discourse.dry-rb.org/t/caching-data-per-call-with-command-pattern/1248>\
**Category:** Uncategorized\
**Created:** [April 20, 2021, 3:51am UTC](https://discourse.dry-rb.org/t/caching-data-per-call-with-command-pattern/1248 "2021-04-20T03:51:28Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![isikyus](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/isikyus/32/464_2.png) [@isikyus](https://discourse.dry-rb.org/u/isikyus)\
**Post date:** [April 20, 2021, 3:51am UTC](https://discourse.dry-rb.org/t/caching-data-per-call-with-command-pattern/1248/1 "2021-04-20T03:51:28Z")

</div>

We’ve got a lot of classes following this general command pattern:

```ruby
class SomeCommand
  import ['subcommand_a', 'subcommand_b']

  def call(args)
    dependency_a.(...)
    # ... processing specific to this command ...
    dependency_b.(...)
    # ...
  end
end

```

There are a lot of things I like about this pattern. It’s easy to test with dependency injection, I can re-use the subcommands elsewhere cleanly, and because there’s no per-call state in the object, dry-system can (could?) memoise and re-use it.

But sometimes `subcommand_a` and `subcommand_b` will both call the same expensive operation (e.g. a remote API request). Ideally I’d like to run this once, and use the result in both places.  
I could add an argument, like so:

```ruby
  def call(id, args)
    cached = expensive_operation(id)
    dependency_a.(..., cached)
    # ... 
    dependency_b.(..., cached)
    # ...
  end

```

But if there’s a lot of existing arguments, this gets quite verbose. It also breaks abstraction: if A calls B, B calls C, and C’s implementation changes to need `expensive_operation`, all of them need extra parameters.

I’ve also considered a full-fledged cache in the `expensive_operation` code, but that raises the spectre of cache expiry. Right now I only keep the expensive data for one call so I haven’t had to worry about that.

How have other people handled this situation? Is there some principle or tool I’m missing that would help?

---

<div class="post-metadata">

**Author:** ![solnic](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/solnic/32/779_2.png) [@solnic](https://discourse.dry-rb.org/u/solnic)\
**Post date:** [April 20, 2021, 6:59am UTC](https://discourse.dry-rb.org/t/caching-data-per-call-with-command-pattern/1248/2 "2021-04-20T06:59:22Z")

</div>

Check out dry-effects - this is what I’d use for caching within a request cycle. There’s an effect called [Cache](https://dry-rb.org/gems/dry-effects/0.1/effects/cache/) that you can use easily, just plug it in and you’re done.

---

<div class="post-metadata">

**Author:** ![isikyus](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/isikyus/32/464_2.png) [@isikyus](https://discourse.dry-rb.org/u/isikyus)\
**Post date:** [March 25, 2022, 5:45am UTC](https://discourse.dry-rb.org/t/caching-data-per-call-with-command-pattern/1248/3 "2022-03-25T05:45:29Z")

</div>

This is perfect, thank you — I’ve got dry-effects caching working in my code now
