File: README.md

package info (click to toggle)
ruby-request-store 1.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 116 kB
  • ctags: 33
  • sloc: ruby: 144; makefile: 2
file content (92 lines) | stat: -rw-r--r-- 2,596 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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# RequestStore [![build status](https://travis-ci.org/steveklabnik/request_store.png?branch=master)](https://travis-ci.org/steveklabnik/request_store) [![Code Climate](https://codeclimate.com/github/steveklabnik/request_store.png)](https://codeclimate.com/github/steveklabnik/request_store)

Ever needed to use a global variable in Rails? Ugh, that's the worst. If you
need global state, you've probably reached for `Thread.current`. Like this:

```ruby
def self.foo
  Thread.current[:foo] ||= 0
end

def self.foo=(value)
  Thread.current[:foo] = value
end
```

Ugh! I hate it. But you gotta do what you gotta do...

### The problem

Everyone's worrying about concurrency these days. So people are using those
fancy threaded web servers, like Thin or Puma. But if you use `Thread.current`,
and you use one of those servers, watch out! Values can stick around longer
than you'd expect, and this can cause bugs. For example, if we had this in
our controller:

```ruby
def index
  Thread.current[:counter] ||= 0
  Thread.current[:counter] += 1
  
  render :text => Thread.current[:counter]
end
```

If we ran this on MRI with Webrick, you'd get `1` as output, every time. But if
you run it with Thin, you get `1`, then `2`, then `3`...

### The solution

Add this line to your application's Gemfile:

```ruby
gem 'request_store'
```

And change the code to this:

```ruby
def index
  RequestStore.store[:foo] ||= 0
  RequestStore.store[:foo] += 1

  render :text => RequestStore.store[:foo]
end
```

Yep, everywhere you used `Thread.current` just change it to
`RequestStore.store`. Now no matter what server you use, you'll get `1` every
time: the storage is local to that request.

### Rails 2 compatibility

The gem includes a Railtie that will configure everything properly for Rails 3+
apps, but if your app is tied to an older (2.x) version, you will have to
manually add the middleware yourself.  Typically this should just be a matter
of adding:

```ruby
config.middleware.use RequestStore::Middleware
```

into your config/environment.rb.

### No Rails? No Problem!

A Railtie is added that configures the Middleware for you, but if you're not
using Rails, no biggie! Just use the Middleware yourself, however you need.
You'll probably have to shove this somewhere:

```ruby
use RequestStore::Middleware
```

## Contributing

1. Fork it
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Commit your changes (`git commit -am 'Add some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
5. Create new Pull Request

Don't forget to run the tests with `rake`.