Sha256: 863fc9481d13f6712f0f03da6d1f9786931508d27a489790500816cfce2ff196

Contents?: true

Size: 1.98 KB

Versions: 5

Compression:

Stored size: 1.98 KB

Contents

# Caching Interceptor

Add the cache interceptor to your basic set of LHC interceptors.

```ruby
  LHC.config.interceptors = [LHC::Caching]
```

You can configure your own cache (default Rails.cache) and logger (default Rails.logger):

```ruby
  LHC::Caching.cache = ActiveSupport::Cache::MemoryStore.new
  LHC::Caching.logger = Logger.new(STDOUT)
```

Caching is not enabled by default, although you added it to your basic set of interceptors.
If you want to have requests served/stored and stored in/from cache, you have to enable it by request.

```ruby
  LHC.get('http://local.ch', cache: true)
```

You can also enable caching when configuring an endpoint in LHS.

```ruby
  class Feedbacks < LHS::Service
    endpoint ':datastore/v2/feedbacks', cache: true
  end
```

Only GET requests are cached by default. If you want to cache any other request method, just configure it:

```ruby
  LHC.get('http://local.ch', cache: { methods: [:get] })
```

## Options

```ruby
  LHC.get('http://local.ch', cache: { key: 'key' expires_in: 1.day, race_condition_ttl: 15.seconds, use: ActiveSupport::Cache::MemoryStore.new })
```

`expires_in` - lets the cache expires every X seconds.

`key` - Set the key that is used for caching by using the option. Every key is prefixed with `LHC_CACHE(v1): `.

`race_condition_ttl` - very useful in situations where a cache entry is used very frequently and is under heavy load.
If a cache expires and due to heavy load several different processes will try to read data natively and then they all will try to write to cache.
To avoid that case the first process to find an expired cache entry will bump the cache expiration time by the value set in `cache_race_condition_ttl`.

`use` - Set an explicit cache to be used for this request. If this option is missing `LHC::Caching.cache` is used.

## Testing

Add to your spec_helper.rb:

```ruby
  require 'lhc/test/cache_helper.rb'
```

This will initialize a MemoryStore cache for LHC::Caching interceptor and resets the cache before every test.

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
lhc-6.5.0 docs/interceptors/caching.md
lhc-6.4.0 docs/interceptors/caching.md
lhc-6.3.1 docs/interceptors/caching.md
lhc-6.3.0 docs/interceptors/caching.md
lhc-6.2.0 docs/interceptors/caching.md