summaryrefslogtreecommitdiffstats
path: root/content/posts/testing-rails-cache/index.org
diff options
context:
space:
mode:
authorDante Catalfamo2023-04-28 00:19:04 -0400
committerDante Catalfamo2023-04-28 00:19:04 -0400
commitfe6fe7b014898f684462a09b20f8fa902510e88d (patch)
tree4d2ab587dbc0be8708cb12ed64e6f00b69af0dba /content/posts/testing-rails-cache/index.org
parent9344862d7452610ca744a93a8ab9a49038ae2561 (diff)
downloadblog-fe6fe7b014898f684462a09b20f8fa902510e88d.tar.gz
blog-fe6fe7b014898f684462a09b20f8fa902510e88d.tar.bz2
blog-fe6fe7b014898f684462a09b20f8fa902510e88d.zip
rails-test-cache: Add post
Diffstat (limited to 'content/posts/testing-rails-cache/index.org')
-rw-r--r--content/posts/testing-rails-cache/index.org33
1 files changed, 33 insertions, 0 deletions
diff --git a/content/posts/testing-rails-cache/index.org b/content/posts/testing-rails-cache/index.org
new file mode 100644
index 0000000..266a900
--- /dev/null
+++ b/content/posts/testing-rails-cache/index.org
@@ -0,0 +1,33 @@
+#+TITLE: Testing Rails Components That Require Cache
+#+DATE: 2023-04-28T00:14:14-04:00
+#+DRAFT: false
+#+DESCRIPTION: How to test rails components that require cache without caching everything
+#+TAGS[]: ruby rails cache
+#+KEYWORDS[]: ruby rails cache
+#+SLUG:
+#+SUMMARY:
+
+If you're in the default testing environment your rails cache is
+=ActiveSupport::Cache::NullStore= which will always succeed but it
+doesn't actually store or return anything.
+
+There's an almost completely undocumented function called
+=with_local_cache= that gets implemented on =NullStore= that lets
+you run a block with a =MemoryStore= instead of a =NullStore=. This
+happens because it =prepends= the
+=ActiveSupport::Cache::Strategy::LocalCache= class.
+
+#+begin_src ruby
+ Rails.cache.class.name
+ # => "ActiveSupport::Cache::NullStore"
+ Rails.cache.write("a", 3)
+ # => true
+ Rails.cache.read("a")
+ # => nil
+ Rails.cache.with_local_cache do
+ Rails.cache.write("a", 5)
+ p Rails.cache.read("a")
+ end
+ 5
+ # => 5
+#+end_src