Class: HTTP::Session::Options::CacheOption

Inherits:
Object
  • Object
show all
Defined in:
lib/http/session/options/cache_option.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ CacheOption

Returns a new instance of CacheOption.

Parameters:

  • options (Hash)

Options Hash (options):

  • :private (Boolean)

    set true if it is a private cache

  • :shared (Boolean)

    set true if it is a shared cache

  • :store (ActiveSupport::Cache::Store)


12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/http/session/options/cache_option.rb', line 12

def initialize(options)
  options =
    case options
    when nil, false then {enabled: false}
    when true then {enabled: true}
    else options
    end

  # Enabled / Disabled
  @enabled = options.fetch(:enabled, true)

  # Shared Cache / Private Cache
  @shared =
    if options.key?(:shared)
      raise ArgumentError, ":shared and :private cannot be used at the same time" if options.key?(:private)
      !!options[:shared]
    elsif options.key?(:private)
      !options[:private]
    else
      true
    end

  # Cache Store
  @store =
    if @enabled
      store = options[:store]
      if store.respond_to?(:read) && store.respond_to?(:write)
        store
      else
        lookup_store(store)
      end
    end
end

Instance Attribute Details

#storeObject (readonly)

Returns the value of attribute store.



6
7
8
# File 'lib/http/session/options/cache_option.rb', line 6

def store
  @store
end

Instance Method Details

#enabled?Boolean

Indicates whether or not the session cache feature is enabled.

Returns:

  • (Boolean)


47
48
49
# File 'lib/http/session/options/cache_option.rb', line 47

def enabled?
  @enabled
end

#private_cache?Boolean

True when it is a private cache.

Private Cache that exists in the client. It is also called local cache or browser cache. It can store and reuse personalized content for a single user.

Returns:

  • (Boolean)


63
64
65
# File 'lib/http/session/options/cache_option.rb', line 63

def private_cache?
  !shared_cache?
end

#shared_cache?Boolean

True when it is a shared cache.

Shared Cache that exists between the origin server and clients (e.g. Proxy, CDN). It stores a single response and reuses it with multiple users

Returns:

  • (Boolean)


55
56
57
# File 'lib/http/session/options/cache_option.rb', line 55

def shared_cache?
  @shared
end