class ActiveSupport::Cache::ElasticsearchStore

A cache store implementation which stores data in ElasticSearch: www.elasticsearch.org/

This store is experimental and was developed against a non-clustered ElasticSearch environment.

Public Class Methods

new(*addresses) click to toggle source

Creates a new ElasticstoreSearch object, with the given elasticsearch server addresses. The addresses and the randomize_hosts, retry_on_failure and reload_connections options are passed verbatim to Elasticsearch::Client.new.

Defaults to ‘localhost:9200’ if no addresses are specified.

Will create an index named ‘cache’ unless the :index_name option is specified.

Calls superclass method
# File lib/activesupport/cache/elasticsearch_store.rb, line 35
def initialize(*addresses)
  addresses = addresses.flatten
  options = addresses.extract_options!

  es_options = options.extract!(:retry_on_failure, :randomize_hosts, :reload_connections)
  es_options.delete_if { |k, v| v.nil? }
  @index_name = options.delete(:index_name) || 'cache'

  if options[:namespace]
    raise ArgumentError, ":namespace option not yet supported"
  end

  super(options)

  addresses = %w(localhost:9200) if addresses.length == 0
  es_options[:addresses] = addresses

  @client = Elasticsearch::Client.new(es_options)
  @index_created = false
end

Public Instance Methods

clear(_options = nil) click to toggle source

Clear the entire cache in our Elasticsearch index. This method should be used with care when shared cache is being used.

# File lib/activesupport/cache/elasticsearch_store.rb, line 58
def clear(_options = nil)
  # TODO: Support namespaces
  @client.delete_by_query index: @index_name, type: 'entry', body: { query: { match_all: {} } }
end

Private Instance Methods

create_index() click to toggle source
# File lib/activesupport/cache/elasticsearch_store.rb, line 98
def create_index
  @index_created = true

  # TODO: Check index mappings to make sure they match ours, warn if they don't.
  @client.indices.create index: @index_name, body: {
    settings: { index: {
      number_of_replicas: 0
    } },
    mappings: { entry: { properties: {
      _ttl: { enabled: true },
      value: { type: 'string', index: 'no' }
    } } }
  }
rescue Elasticsearch::Transport::Transport::Errors::BadRequest => e
  raise unless e.message =~ /IndexAlreadyExists/
end