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



16
17
18
19
20
21
22
23
24
25
# File 'lib/mondrian_redis_segment_cache/cache.rb', line 16

def initialize(mondrian_redis_connection, new_options = {})
  @mondrian_redis = mondrian_redis_connection
  @created_listener_connection = ::Redis.new(client_options)
  @deleted_listener_connection = ::Redis.new(client_options)
  @options = Marshal.load(Marshal.dump(new_options))

  reset_listeners
  register_for_redis_events
  reconcile_set_and_keys
end

Instance Attribute Details

#created_listener_connection ⇒ Object (readonly)

Returns the value of attribute created_listener_connection.



9
10
11
# File 'lib/mondrian_redis_segment_cache/cache.rb', line 9

def created_listener_connection
  @created_listener_connection
end

#deleted_listener_connection ⇒ Object (readonly)

Returns the value of attribute deleted_listener_connection.



9
10
11
# File 'lib/mondrian_redis_segment_cache/cache.rb', line 9

def deleted_listener_connection
  @deleted_listener_connection
end

#listeners ⇒ Object (readonly)

Returns the value of attribute listeners.



9
10
11
# File 'lib/mondrian_redis_segment_cache/cache.rb', line 9

def listeners
  @listeners
end

#mondrian_redis ⇒ Object (readonly)

Returns the value of attribute mondrian_redis.



9
10
11
# File 'lib/mondrian_redis_segment_cache/cache.rb', line 9

def mondrian_redis
  @mondrian_redis
end

#options ⇒ Object (readonly)

Returns the value of attribute options.



9
10
11
# File 'lib/mondrian_redis_segment_cache/cache.rb', line 9

def options
  @options
end

Instance Method Details

#addListener(segment_cache_listener) ⇒ Object

Public Instance Methods



30
31
32
33
# File 'lib/mondrian_redis_segment_cache/cache.rb', line 30

def addListener(segment_cache_listener)
  listeners << segment_cache_listener
  eager_load_listener(segment_cache_listener)
end

#contains(segment_header) ⇒ Object

returns boolean takes mondrian.spi.SegmentHeader



37
38
39
40
41
42
43
44
45
46
# File 'lib/mondrian_redis_segment_cache/cache.rb', line 37

def contains(segment_header)
  segment_header.description # Hazel adapter says this affects serialization
  header_base64 = segment_header_to_base64(segment_header)

  if header_base64
    return mondrian_redis.exists(header_base64)
  end

  return false
end

#created_event_key ⇒ Object



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

def created_event_key
  @created_event_key ||= "__keyevent@#{client_options[:db]}__:set"
end

#deleted_event_key ⇒ Object



52
53
54
# File 'lib/mondrian_redis_segment_cache/cache.rb', line 52

def deleted_event_key
  @deleted_event_key ||= "__keyevent@#{client_options[:db]}__:del"
end

#eager_load_listener(listener) ⇒ Object



64
65
66
67
68
# File 'lib/mondrian_redis_segment_cache/cache.rb', line 64

def eager_load_listener(listener)
  mondrian_redis.sscan_each(SEGMENT_HEADERS_SET_KEY) do |segment_header_base64|
    publish_created_to_listener(segment_header_base64, listener)
  end
end

#evicted_event_key ⇒ Object



56
57
58
# File 'lib/mondrian_redis_segment_cache/cache.rb', line 56

def evicted_event_key
  @evicted_event_key ||= "__keyevent@#{client_options[:db]}__:evicted"
end

#expired_event_key ⇒ Object



60
61
62
# File 'lib/mondrian_redis_segment_cache/cache.rb', line 60

def expired_event_key
  @expired_event_key ||= "__keyevent@#{client_options[:db]}__:expired"
end

#get(segment_header) ⇒ Object

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



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

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

  if header_base64
    body_base64 = mondrian_redis.get(header_base64)
    return segment_body_from_base64(body_base64)
  end

  return nil
end

#getSegmentHeaders ⇒ Object

returns ArrayList



85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/mondrian_redis_segment_cache/cache.rb', line 85

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

  mondrian_redis.sscan_each(SEGMENT_HEADERS_SET_KEY) do |segment_header_base64|
    segment_header = segment_header_from_base64(segment_header_base64)

    if segment_header
      segment_headers << segment_header
    end
  end

  return segment_headers
end

#publish_created_to_listener(message, listener) ⇒ Object



99
100
101
102
103
104
105
106
# File 'lib/mondrian_redis_segment_cache/cache.rb', line 99

def publish_created_to_listener(message, listener)
  segment_header = segment_header_from_base64(message)

  if segment_header
    created_event = ::MondrianRedisSegmentCache::CreatedEvent.new(segment_header)
    listener.handle(created_event)
  end
end

#publish_created_to_listeners(message) ⇒ Object



108
109
110
111
112
# File 'lib/mondrian_redis_segment_cache/cache.rb', line 108

def publish_created_to_listeners(message)
  listeners.each do |listener|
    publish_created_to_listener(message, listener)
  end
end

#publish_deleted_to_listener(message, listener) ⇒ Object



114
115
116
117
118
119
120
121
# File 'lib/mondrian_redis_segment_cache/cache.rb', line 114

def publish_deleted_to_listener(message, listener)
  segment_header = segment_header_from_base64(message)

  if segment_header
    deleted_event = ::MondrianRedisSegmentCache::DeletedEvent.new(segment_header)
    listener.handle(deleted_event)
  end
end

#publish_deleted_to_listeners(message) ⇒ Object Also known as: publish_evicted_to_listeners, publish_expired_to_listeners



123
124
125
126
127
128
129
130
131
132
# File 'lib/mondrian_redis_segment_cache/cache.rb', line 123

def publish_deleted_to_listeners(message)
  listeners.each do |listener|
    publish_deleted_to_listener(message, listener)
  end

  # Each server can tell the Set to remove the key as it may be expired
  if mondrian_redis.sismember(SEGMENT_HEADERS_SET_KEY, message)
    mondrian_redis.srem(SEGMENT_HEADERS_SET_KEY, message)
  end
end

#put(segment_header, segment_body) ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/mondrian_redis_segment_cache/cache.rb', line 136

def put(segment_header, segment_body)
  segment_header.description # Hazel adapter says this affects serialization
  header_base64 = segment_header_to_base64(segment_header)
  body_base64 = segment_body_to_base64(segment_body)
  mondrian_redis.sadd(SEGMENT_HEADERS_SET_KEY, header_base64)
  
  if options[:ttl]
    set_success = mondrian_redis.setex(header_base64, options[:ttl], body_base64)
  else
    set_success = mondrian_redis.set(header_base64, body_base64)
  end

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

#remove(segment_header) ⇒ Object



151
152
153
154
155
156
157
158
# File 'lib/mondrian_redis_segment_cache/cache.rb', line 151

def remove(segment_header)
  segment_header.description # Hazel adapter says this affects serialization
  header_base64 = segment_header_to_base64(segment_header)
  mondrian_redis.srem(SEGMENT_HEADERS_SET_KEY, header_base64)
  deleted_keys = mondrian_redis.del(header_base64)

  return deleted_keys >= 1
end

#removeListener(segment_cache_listener) ⇒ Object



160
161
162
# File 'lib/mondrian_redis_segment_cache/cache.rb', line 160

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

#reset_listeners ⇒ Object



164
165
166
# File 'lib/mondrian_redis_segment_cache/cache.rb', line 164

def reset_listeners
  @listeners = Set.new
end

#supportsRichIndex ⇒ Object



168
169
170
# File 'lib/mondrian_redis_segment_cache/cache.rb', line 168

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

#tearDown ⇒ Object



172
173
174
175
176
177
178
179
180
181
# File 'lib/mondrian_redis_segment_cache/cache.rb', line 172

def tearDown()
  if options[:delete_all_on_tear_down]
    # Remove all of the headers and the set that controls them
    mondrian_redis.sscan_each(SEGMENT_HEADERS_SET_KEY) do |segment_header_base64|
      mondrian_redis.del(segment_header_base64)
    end

    mondrian_redis.del(SEGMENT_HEADERS_SET_KEY)
  end
end