Skip to content

Commit 8a40ce7

Browse files
committed
Correct config database
1. Use the DATABASE environment variable to match the name used in the installer 2. Default to "cache" which will match what Rails generators will use
1 parent 94ffc03 commit 8a40ce7

File tree

3 files changed

+45
-51
lines changed

3 files changed

+45
-51
lines changed

README.md

Lines changed: 43 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,7 @@ Solid Cache is a database-backed Active Support cache store implementation.
66

77
Using SQL databases backed by SSDs we can have caches that are much larger and cheaper than traditional memory-only Redis or Memcached backed caches.
88

9-
## Usage
10-
11-
To set Solid Cache as your Rails cache, you should add this to your environment config:
12-
13-
```ruby
14-
config.cache_store = :solid_cache_store
15-
```
9+
## Introduction
1610

1711
Solid Cache is a FIFO (first in, first out) cache. While this is not as efficient as an LRU cache, it is mitigated by the longer cache lifespan.
1812

@@ -21,7 +15,7 @@ A FIFO cache is much easier to manage:
2115
2. We can estimate and control the cache size by comparing the maximum and minimum IDs.
2216
3. By deleting from one end of the table and adding at the other end we can avoid fragmentation (on MySQL at least).
2317

24-
### Installation
18+
## Installation
2519
Add this line to your application's `Gemfile`:
2620

2721
```ruby
@@ -38,18 +32,56 @@ Or install it yourself as:
3832
$ gem install solid_cache
3933
```
4034

35+
#### Cache database configuration
36+
37+
The default installation of Solid Cache expects a database named `cache` in `database.yml`. It should
38+
have it's own connection pool to avoid mixing cache queries in other transactions.
39+
40+
You can use the primary database for your cache like this:
41+
42+
```yaml
43+
# config/database.yml
44+
production:
45+
primary: &production_primary
46+
...
47+
cache:
48+
<<: *production_primary
49+
```
50+
51+
Or a separate database like this:
52+
53+
```yaml
54+
production:
55+
primary:
56+
...
57+
cache:
58+
database: cache_development
59+
host: 127.0.0.1
60+
migrations_paths: "db/cache/migrate"
61+
```
62+
63+
#### Install Solid Cache
64+
4165
Now, you need to install the necessary migrations and configure the cache store. You can do both at once using the provided generator:
4266
4367
```bash
68+
# If using the primary database
4469
$ bin/rails generate solid_cache:install
70+
71+
# Or if using a dedicated database
72+
$ DATABASE=cache bin/rails generate solid_cache:install
4573
```
4674

4775
This will set solid_cache as the cache store in production, and will copy the optional configuration file and the required migration over to your app.
4876

4977
Alternatively, you can add only the migration to your app:
5078

5179
```bash
52-
$ bin/rails solid_cache:install:migrations
80+
# If using the primary database
81+
$ bin/rails generate solid_cache:install:migrations
82+
83+
# Or if using a dedicated database
84+
$ DATABASE=cache bin/rails generate solid_cache:install:migrations
5385
```
5486

5587
And set Solid Cache as your application's cache store backend manually, in your environment config:
@@ -59,6 +91,8 @@ And set Solid Cache as your application's cache store backend manually, in your
5991
config.cache_store = :solid_cache_store
6092
```
6193

94+
#### Run migrations
95+
6296
Finally, you need to run the migrations:
6397

6498
```bash
@@ -168,46 +202,6 @@ Only triggering expiry when we write means that if the cache is idle, the backgr
168202

169203
If you want the cache expiry to be run in a background job instead of a thread, you can set `expiry_method` to `:job`. This will enqueue a `SolidCache::ExpiryJob`.
170204

171-
### Using a dedicated cache database
172-
173-
Add database configuration to database.yml, e.g.:
174-
175-
```
176-
development:
177-
cache:
178-
database: cache_development
179-
host: 127.0.0.1
180-
migrations_paths: "db/cache/migrate"
181-
```
182-
183-
Create database:
184-
```
185-
$ bin/rails db:create
186-
```
187-
188-
Install migrations:
189-
```
190-
$ bin/rails solid_cache:install:migrations
191-
```
192-
193-
Move migrations to custom migrations folder:
194-
```
195-
$ mkdir -p db/cache/migrate
196-
$ mv db/migrate/*.solid_cache.rb db/cache/migrate
197-
```
198-
199-
Set the engine configuration to point to the new database:
200-
```yaml
201-
# config/solid_cache.yml
202-
production:
203-
database: cache
204-
```
205-
206-
Run migrations:
207-
```
208-
$ bin/rails db:migrate
209-
```
210-
211205
### Sharding the cache
212206

213207
Solid Cache uses the [Maglev](https://static.googleusercontent.com/media/research.google.com/en//pubs/archive/44824.pdf) consistent hashing scheme to shard the cache across multiple databases.

lib/generators/solid_cache/install/templates/config/solid_cache.yml.tt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
default: &default
2-
database: <%%= Rails.env %>
2+
database: <%= ENV.fetch("DATABASE", "cache") %>
33
store_options:
44
max_age: <%%= 1.week.to_i %>
55
max_size: <%%= 256.megabytes %>

test/lib/generators/solid_cache/solid_cache/install_generator_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class SolidCache::InstallGeneratorTest < Rails::Generators::TestCase
2929
def expected_config
3030
<<~YAML
3131
default: &default
32-
database: <%= Rails.env %>
32+
database: cache
3333
store_options:
3434
max_age: <%= 1.week.to_i %>
3535
max_size: <%= 256.megabytes %>

0 commit comments

Comments
 (0)