Skip to content

Commit 69fb86c

Browse files
committed
Update Readme for v0.5
Update readme with the new configuration format
1 parent 1857cf5 commit 69fb86c

File tree

6 files changed

+57
-70
lines changed

6 files changed

+57
-70
lines changed

README.md

+39-66
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
11
# Solid Cache
22

3-
**Upgrading from v0.3.0 or earlier? Please see [upgrading to version 0.4.0](upgrading_to_version_0.4.x.md)**
3+
**Upgrading from v0.3.0 or earlier? Please see [upgrading to version v0.4.x and beyond](upgrading_to_version_0.4.x.md)**
44

55
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-
Testing on [HEY](https://hey.com) shows that reads and writes are 25%-50% slower than with a Redis cache (1.2ms vs 0.8-1ms per single-key read), but this is not a significant percentage of the overall request time.
10-
11-
If cache misses are expensive (up to 50x the cost of a hit on HEY), then there are big advantages to caches that can hold months rather than days of data.
12-
139
## Usage
1410

1511
To set Solid Cache as your Rails cache, you should add this to your environment config:
@@ -18,7 +14,7 @@ To set Solid Cache as your Rails cache, you should add this to your environment
1814
config.cache_store = :solid_cache_store
1915
```
2016

21-
Solid Cache is a FIFO (first in, first out) cache. While this is not as efficient as an LRU cache, this is mitigated by the longer cache lifespans.
17+
Solid Cache is a FIFO (first in, first out) cache. While this is not as efficient as an LRU cache, this is mitigated by the longer cache lifespan.
2218

2319
A FIFO cache is much easier to manage:
2420
1. We don't need to track when items are read
@@ -55,7 +51,7 @@ $ bin/rails db:migrate
5551

5652
### Configuration
5753

58-
Configuration will be read from solid_cache.yml. You can change the location of the config file by setting the `SOLID_CACHE_CONFIG` env variable.
54+
Configuration will be read from `config/solid_cache.yml`. You can change the location of the config file by setting the `SOLID_CACHE_CONFIG` env variable.
5955

6056
The format of the file is:
6157

@@ -99,9 +95,12 @@ SolidCache::Record.connects_to shards: { cache_db1: { writing: :cache_db1 }, ca
9995

10096
If `connects_to` is set it will be passed directly.
10197

98+
If none of these are set, then Solid Cache will use the `ActiveRecord::Base` connection pool. This means that cache reads and writes will be part of any wrapping
99+
database transaction.
100+
102101
#### Engine configuration
103102

104-
There are two options that can be set on the engine:
103+
There are three options that can be set on the engine:
105104

106105
- `executor` - the [Rails executor](https://guides.rubyonrails.org/threading_and_code_execution.html#executor) used to wrap asynchronous operations, defaults to the app executor
107106
- `connects_to` - a custom connects to value for the abstract `SolidCache::Record` active record model. Required for sharding and/or using a separate cache database to the main app. This will overwrite any value set in `config/solid_cache.yml`
@@ -111,12 +110,7 @@ These can be set in your Rails configuration:
111110

112111
```ruby
113112
Rails.application.configure do
114-
config.solid_cache.connects_to = {
115-
shards: {
116-
shard1: { writing: :cache_primary_shard1 },
117-
shard2: { writing: :cache_primary_shard2 }
118-
}
119-
}
113+
config.solid_cache.size_estimate_samples = 1000
120114
end
121115
```
122116

@@ -184,10 +178,10 @@ $ mv db/migrate/*.solid_cache.rb db/cache/migrate
184178
```
185179

186180
Set the engine configuration to point to the new database:
187-
```
188-
Rails.application.configure do
189-
config.solid_cache.connects_to = { database: { writing: :cache } }
190-
end
181+
```yaml
182+
# config/solid_cache.yml
183+
production:
184+
database: cache
191185
```
192186

193187
Run migrations:
@@ -220,19 +214,10 @@ production:
220214
host: cache3-db
221215
```
222216

223-
```ruby
224-
# config/environment/production.rb
225-
Rails.application.configure do
226-
config.solid_cache.connects_to = {
227-
shards: {
228-
cache_shard1: { writing: :cache_shard1 },
229-
cache_shard2: { writing: :cache_shard2 },
230-
cache_shard3: { writing: :cache_shard3 },
231-
}
232-
}
233-
234-
config.cache_store = [ :solid_cache_store, cluster: { shards: [ :cache_shard1, :cache_shard2, :cache_shard3 ] } ]
235-
end
217+
```yaml
218+
# config/solid_cache.yml
219+
production:
220+
databases: [cache_shard1, cache_shard2, cache_shard3]
236221
```
237222

238223
### Secondary cache clusters
@@ -243,21 +228,14 @@ Writes will go to all clusters. The writes to the primary cluster are synchronou
243228

244229
To specific multiple clusters you can do:
245230

246-
```ruby
247-
Rails.application.configure do
248-
config.solid_cache.connects_to = {
249-
shards: {
250-
cache_primary_shard1: { writing: :cache_primary_shard1 },
251-
cache_primary_shard2: { writing: :cache_primary_shard2 },
252-
cache_secondary_shard1: { writing: :cache_secondary_shard1 },
253-
cache_secondary_shard2: { writing: :cache_secondary_shard2 },
254-
}
255-
}
256-
257-
primary_cluster = { shards: [ :cache_primary_shard1, :cache_primary_shard2 ] }
258-
secondary_cluster = { shards: [ :cache_secondary_shard1, :cache_secondary_shard2 ] }
259-
config.cache_store = [ :solid_cache_store, clusters: [ primary_cluster, secondary_cluster ] ]
260-
end
231+
```yaml
232+
# config/solid_cache.yml
233+
production:
234+
databases: [cache_primary_shard1, cache_primary_shard2, cache_secondary_shard1, cache_secondary_shard2]
235+
store_options:
236+
clusters:
237+
- shards: [cache_primary_shard1, cache_primary_shard2]
238+
- shards: [cache_secondary_shard1, cache_secondary_shard2]
261239
```
262240

263241
### Named shard destinations
@@ -266,24 +244,19 @@ By default, the node key used for sharding is the name of the database in `datab
266244

267245
It is possible to add names for the shards in the cluster config. This will allow you to shuffle or remove shards without breaking consistent hashing.
268246

269-
```ruby
270-
Rails.application.configure do
271-
config.solid_cache.connects_to = {
272-
shards: {
273-
cache_primary_shard1: { writing: :cache_primary_shard1 },
274-
cache_primary_shard2: { writing: :cache_primary_shard2 },
275-
cache_secondary_shard1: { writing: :cache_secondary_shard1 },
276-
cache_secondary_shard2: { writing: :cache_secondary_shard2 },
277-
}
278-
}
279-
280-
primary_cluster = { shards: { cache_primary_shard1: :node1, cache_primary_shard2: :node2 } }
281-
secondary_cluster = { shards: { cache_primary_shard1: :node3, cache_primary_shard2: :node4 } }
282-
config.cache_store = [ :solid_cache_store, clusters: [ primary_cluster, secondary_cluster ] ]
283-
end
247+
```yaml
248+
production:
249+
databases: [cache_primary_shard1, cache_primary_shard2, cache_secondary_shard1, cache_secondary_shard2]
250+
store_options:
251+
clusters:
252+
- shards:
253+
cache_primary_shard1: node1
254+
cache_primary_shard2: node2
255+
- shards:
256+
cache_secondary_shard1: node3
257+
cache_secondary_shard2: node4
284258
```
285259

286-
287260
### Enabling encryption
288261

289262
Add this to an initializer:
@@ -302,7 +275,7 @@ The Solid Cache migrations try to create an index with 1024 byte entries. If tha
302275

303276
## Development
304277

305-
Run the tests with `bin/rails test`. By default, these will run against SQLite.
278+
Run the tests with `bin/rake test`. By default, these will run against SQLite.
306279

307280
You can also run the tests against MySQL and PostgreSQL. First start up the databases:
308281

@@ -321,8 +294,8 @@ $ TARGET_DB=postgres bin/rails db:setup
321294
Then run the tests for the target database:
322295

323296
```shell
324-
$ TARGET_DB=mysql bin/rails test
325-
$ TARGET_DB=postgres bin/rails test
297+
$ TARGET_DB=mysql bin/rake test
298+
$ TARGET_DB=postgres bin/rake test
326299
```
327300

328301
### Testing with multiple Rails version
@@ -333,7 +306,7 @@ multiple Rails version.
333306
To run a test for a specific version run:
334307

335308
```shell
336-
bundle exec appraisal rails-7-1 bin/rails test
309+
bundle exec appraisal rails-7-1 bin/rake test
337310
```
338311

339312
After updating the dependencies in the `Gemfile` please run:

Rakefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def run_without_aborting(*tasks)
2323
end
2424

2525
def configs
26-
[ :default, :cluster, :clusters, :clusters_named, :database, :no_database ]
26+
[ :default, :cluster, :cluster_inferred, :clusters, :clusters_named, :database, :no_database ]
2727
end
2828

2929
task :test do

solid_cache.gemspec

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Gem::Specification.new do |spec|
1313
spec.license = "MIT"
1414

1515
spec.post_install_message = <<~MESSAGE
16-
Solid Cache v0.4 contains new database migrations.
16+
Upgrading from Solid Cache v0.3 or earlier? There are new database migrations in v0.4.
1717
See https://github.com/rails/solid_cache/blob/main/upgrading_to_version_0.4.x.md for upgrade instructions.
1818
MESSAGE
1919

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
default: &default
2+
databases: [primary_shard_one, primary_shard_two]
3+
4+
store_options:
5+
max_age: 3600
6+
max_size:
7+
8+
development:
9+
<<: *default
10+
11+
test:
12+
<<: *default

test/models/solid_cache/record_test.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class RecordTest < ActiveSupport::TestCase
1111
assert_equal [ :default ], shards
1212
when "config/solid_cache_clusters.yml", "config/solid_cache_clusters_named.yml", nil
1313
assert_equal [ :primary_shard_one, :primary_shard_two, :secondary_shard_one, :secondary_shard_two ], shards
14-
when "config/solid_cache_cluster.yml"
14+
when "config/solid_cache_cluster.yml", "config/solid_cache_cluster_inferred.yml"
1515
assert_equal [ :primary_shard_one, :primary_shard_two ], shards
1616
else
1717
raise "Unknown SOLID_CACHE_CONFIG: #{ENV["SOLID_CACHE_CONFIG"]}"
@@ -25,7 +25,7 @@ class RecordTest < ActiveSupport::TestCase
2525
assert_equal [ :reading ], role
2626
when "config/solid_cache_clusters.yml", "config/solid_cache_clusters_named.yml", nil
2727
assert_equal [ :writing, :writing, :writing, :writing ], role
28-
when "config/solid_cache_cluster.yml"
28+
when "config/solid_cache_cluster.yml", "config/solid_cache_cluster_inferred.yml"
2929
assert_equal [ :writing, :writing ], role
3030
else
3131
raise "Unknown SOLID_CACHE_CONFIG: #{ENV["SOLID_CACHE_CONFIG"]}"

upgrading_to_version_0.4.x.md

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
The database schema has been updated in v0.4.0, so you'll need to follow the upgrade steps if you have used v0.3 or lower.
44

5+
**IMPORTANT: You cannot upgrade directly from v0.3.x or lower to v0.5.x or higher, always go via v0.4.x first**
6+
57
## Why has the schema changed?
68

79
There are two new changes in v0.4.x

0 commit comments

Comments
 (0)