Class: MondrianRedisSegmentCache::Cache

Inherits:
Object
  • Object
show all
Includes:
Java::MondrianSpi::SegmentCache
Defined in:
lib/mondrian_redis_segment_cache/cache.rb

Constant Summary collapse

SEGMENT_HEADERS_SET_KEY =
"MONDRIAN_SEGMENT_HEADERS_SET"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mondrian_redis_connection, new_options = {}) ⇒ Cache

Constructor



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/mondrian_redis_segment_cache/cache.rb', line 27

def initialize(mondrian_redis_connection, new_options = {})
  @mondrian_redis = mondrian_redis_connection
  @options = Marshal.load(Marshal.dump(new_options))
  @listeners = SynchronizedSet.new
  @local_cache_set = SynchronizedSet.new

  ##
  # Having a TimerTask reconcile every 6 minutes so the local listeners are eventually consistent with
  # respect to what is in the cache and what has been done .... allows us to get rid of the event
  # subscribers in the redis API ... consider the job to have timed out after 60 seconds
  @reconcile_task = ::Concurrent::TimerTask.new(:execution_interval => 360, :timeout_interval => 60) do
    reload
  end

  @reconcile_task.execute
  reload
end

Instance Attribute Details

#listenersObject (readonly)

Returns the value of attribute listeners.



17
18
19
# File 'lib/mondrian_redis_segment_cache/cache.rb', line 17

def listeners
  @listeners
end

#local_cache_setObject (readonly)

Returns the value of attribute local_cache_set.



17
18
19
# File 'lib/mondrian_redis_segment_cache/cache.rb', line 17

def local_cache_set
  @local_cache_set
end

#mondrian_redisObject (readonly)

Returns the value of attribute mondrian_redis.



17
18
19
# File 'lib/mondrian_redis_segment_cache/cache.rb', line 17

def mondrian_redis
  @mondrian_redis
end

#optionsObject (readonly)

Returns the value of attribute options.



17
18
19
# File 'lib/mondrian_redis_segment_cache/cache.rb', line 17

def options
  @options
end

Instance Method Details

#addListener(segment_cache_listener) ⇒ Object

Public Instance Methods



48
49
50
# File 'lib/mondrian_redis_segment_cache/cache.rb', line 48

def addListener(segment_cache_listener)
  listeners << segment_cache_listener
end

#get(segment_header) ⇒ Object

returns mondrian.spi.SegmentBody takes mondrian.spi.SegmentHeader



54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/mondrian_redis_segment_cache/cache.rb', line 54

def get(segment_header)
  segment_header.getDescription # Hazel adapter says this affects serialization
  header_base64 = segment_header_to_base64(segment_header)

  if header_base64
    body_base64 = mondrian_redis.with do |connection|
      connection.get(header_base64)
    end

    return segment_body_from_base64(body_base64)
  end

  return nil
end

#getSegmentHeadersObject

returns ArrayList<SegmentHeader>



70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/mondrian_redis_segment_cache/cache.rb', line 70

def getSegmentHeaders()
  segment_headers = ::Java::JavaUtil::ArrayList.new

  mondrian_redis.with do |connection|
    connection.sscan_each(SEGMENT_HEADERS_SET_KEY) do |segment_header_base64|
      segment_header = segment_header_from_base64(segment_header_base64)
      segment_headers << segment_header if segment_header
    end
  end

  return segment_headers
end

#put(segment_header, segment_body) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/mondrian_redis_segment_cache/cache.rb', line 83

def put(segment_header, segment_body)
  set_success = nil
  segment_header.getDescription # Hazel adapter says this affects serialization
  header_base64 = segment_header_to_base64(segment_header)
  body_base64 = segment_body_to_base64(segment_body)
  @local_cache_set << header_base64
  mondrian_redis.with do |connection|
    connection.sadd(SEGMENT_HEADERS_SET_KEY, header_base64)
  end

  if has_expiry?
    set_success = mondrian_redis.with do |connection|
      connection.setex(header_base64, expires_in_seconds, body_base64)
    end
  else
    set_success = mondrian_redis.with do |connection|
      connection.set(header_base64, body_base64)
    end
  end

  return ("#{set_success}".upcase == "OK" || set_success == true) # weird polymorphic return ?
end

#reloadObject



106
107
108
109
# File 'lib/mondrian_redis_segment_cache/cache.rb', line 106

def reload
  reconcile_set_and_keys
  reconcile_local_set_with_redis
end

#remove(segment_header) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/mondrian_redis_segment_cache/cache.rb', line 111

def remove(segment_header)
  segment_header.getDescription # Hazel adapter says this affects serialization
  header_base64 = segment_header_to_base64(segment_header)
  mondrian_redis.with do |connection|
    connection.srem(SEGMENT_HEADERS_SET_KEY, header_base64)
  end

  deleted_keys = mondrian_redis.with do |connection|
    connection.del(header_base64)
  end

  return deleted_keys >= 1
end

#removeListener(segment_cache_listener) ⇒ Object



125
126
127
# File 'lib/mondrian_redis_segment_cache/cache.rb', line 125

def removeListener(segment_cache_listener)
  listeners.delete(segment_cache_listener)
end

#supportsRichIndexObject



129
130
131
# File 'lib/mondrian_redis_segment_cache/cache.rb', line 129

def supportsRichIndex()
  true # this is why we are serializing the headers to base64
end

#tearDownObject



133
134
135
# File 'lib/mondrian_redis_segment_cache/cache.rb', line 133

def tearDown()
  #no-op
end