Class: ActiveSupport::Cache::ElasticsearchStore

Inherits:
Store
  • Object
show all
Defined in:
lib/activesupport/cache/elasticsearch_store.rb

Overview

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.

Instance Method Summary collapse

Constructor Details

#initialize(*addresses) ⇒ ElasticsearchStore

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.



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# 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

Instance Method Details

#clear(_options = nil) ⇒ Object

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



58
59
60
61
# 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