Quantcast
Channel: Active questions tagged ruby - Stack Overflow
Viewing all articles
Browse latest Browse all 4636

Don't memoize a variable across multiple http requests

$
0
0

Is it common for a variable to persist across requests when caching is turned off? Can I turn it off?

I've always thought that if I want to persist data between requests, I should use a database or another persistent storage, and memoization is only for saving computation in one request when a method is called multiple times.

My model:

app/models/simple_model.rb:

class SimpleModel < ApplicationRecord  def time    @time ||= Time.now  endend

Across different http requests @time returns the same value. @time.object_id is the same as well.

[1] pry(#<SimpleModel>)> @time=> 2024-04-10 00:43:13.325467 +0200[2] pry(#<SimpleModel>)> @time.object_id=> 14600# -- another request ---[1] pry(#<SimpleModel>)> @time=> 2024-04-10 00:43:13.325467 +0200[2] pry(#<SimpleModel>)> @time.object_id=> 14600

If I restart the server, then @time is new.

My config/environments/development.rb is a default one. Caching is disabled:

    config.action_controller.perform_caching = false    config.cache_store = :null_store

I don't use any specific gems. It's almost vanilla Rails with `

I run rails with rails s. It's running in a development mode:

$ rails s -p 3554=> Booting Puma=> Rails 6.1.5 application starting in development=> Run `bin/rails server --help` for more startup optionsPuma starting in single mode...* Puma version: 5.6.2 (ruby 3.1.3-p185) ("Birdie's Version")*  Min threads: 5*  Max threads: 5*  Environment: development*          PID: 87377* Listening on http://127.0.0.1:3554* Listening on http://[::1]:3554Use Ctrl-C to stop

If it's an expected behavior and my understanding hasn't been right, then in which case is this case invalidated? Is manual intervention the only solution?


Viewing all articles
Browse latest Browse all 4636

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>