File: cache.html.md

package info (click to toggle)
ruby-dry-core 0.7.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 396 kB
  • sloc: ruby: 2,126; sh: 4; makefile: 4
file content (30 lines) | stat: -rw-r--r-- 622 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
---
title: Cache
layout: gem-single
name: dry-core
---

Allows you to cache call results that are solely determined by arguments.

```ruby
require 'dry/core/cache'

class Foo
  extend Dry::Core::Cache

  attr_reader :source

  def initialize(source)
    @source = source
  end

  def heavy_computation(arg1, arg2)
    fetch_or_store(source, arg1, arg2) { source ^ arg1 ^ arg2 }
  end
end
```

### Note

Beware Proc instance hashes are not equal, i.e. `-> { 1 }.hash != -> { 1 }.hash`.
This means you shouldn't pass Procs in args unless you're sure they are always the same instances, otherwise you introduce a memory leak