Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correct config database #194

Merged
merged 1 commit into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 43 additions & 49 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,7 @@ Solid Cache is a database-backed Active Support cache store implementation.

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.

## Usage

To set Solid Cache as your Rails cache, you should add this to your environment config:

```ruby
config.cache_store = :solid_cache_store
```
## Introduction

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.

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

### Installation
## Installation
Add this line to your application's `Gemfile`:

```ruby
Expand All @@ -38,18 +32,56 @@ Or install it yourself as:
$ gem install solid_cache
```

#### Cache database configuration

The default installation of Solid Cache expects a database named `cache` in `database.yml`. It should
have it's own connection pool to avoid mixing cache queries in other transactions.

You can use the primary database for your cache like this:

```yaml
# config/database.yml
production:
primary: &production_primary
...
cache:
<<: *production_primary
```

Or a separate database like this:

```yaml
production:
primary:
...
cache:
database: cache_development
host: 127.0.0.1
migrations_paths: "db/cache/migrate"
```

#### Install Solid Cache

Now, you need to install the necessary migrations and configure the cache store. You can do both at once using the provided generator:

```bash
# If using the primary database
$ bin/rails generate solid_cache:install

# Or if using a dedicated database
$ DATABASE=cache bin/rails generate solid_cache:install
```

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.

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

```bash
$ bin/rails solid_cache:install:migrations
# If using the primary database
$ bin/rails generate solid_cache:install:migrations

# Or if using a dedicated database
$ DATABASE=cache bin/rails generate solid_cache:install:migrations
```

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

#### Run migrations

Finally, you need to run the migrations:

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

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`.

### Using a dedicated cache database

Add database configuration to database.yml, e.g.:

```
development:
cache:
database: cache_development
host: 127.0.0.1
migrations_paths: "db/cache/migrate"
```

Create database:
```
$ bin/rails db:create
```

Install migrations:
```
$ bin/rails solid_cache:install:migrations
```

Move migrations to custom migrations folder:
```
$ mkdir -p db/cache/migrate
$ mv db/migrate/*.solid_cache.rb db/cache/migrate
```

Set the engine configuration to point to the new database:
```yaml
# config/solid_cache.yml
production:
database: cache
```

Run migrations:
```
$ bin/rails db:migrate
```

### Sharding the cache

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.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
default: &default
database: <%%= Rails.env %>
database: <%= ENV.fetch("DATABASE", "cache") %>
store_options:
max_age: <%%= 1.week.to_i %>
max_size: <%%= 256.megabytes %>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class SolidCache::InstallGeneratorTest < Rails::Generators::TestCase
def expected_config
<<~YAML
default: &default
database: <%= Rails.env %>
database: cache
store_options:
max_age: <%= 1.week.to_i %>
max_size: <%= 256.megabytes %>
Expand Down