Class: ActiveSupport::Cache::GemFire

Inherits:
Store
  • Object
show all
Defined in:
lib/gemfire-jruby.rb

Overview

ActiveSupport::Cache::GemFire creates a Singleton object that provides access to a GemFire cache.

Defined Under Namespace

Classes: CacheException

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(role, options) ⇒ GemFire

Returns a new instance of GemFire.



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/gemfire-jruby.rb', line 33

def initialize(role, options)      
   @role = role
   # fill the GemFire properties from the options
   check_required_options(role, options)
   # join the distributed system
   properties = get_gemfire_properties(role, options)
   system = DistributedSystem.connect(properties)
  # create the cache ... this will read a cache.xml
  @cache = CacheFactory.create(system)
   # there is only one region
   regionAttributes = nil
  if(role == 'client') then
    # it's a client
    regionAttributes = get_client_attributes(options)
  else
    # it's a server
     cacheServer = @cache.addCacheServer
     cacheServer.setPort(options['cacheserver-port'])
     cacheServer.start
    regionAttributes = get_server_attributes(options)
  end 
  @region = @cache.createRegion(options['region-name'], regionAttributes)
 rescue CacheException => e
     logger.error("GemfireCache Creation Error (#{e}): #{e.message}")
end

Class Attribute Details

.instanceObject

Returns the value of attribute instance.



18
19
20
# File 'lib/gemfire-jruby.rb', line 18

def instance
  @instance
end

Class Method Details

.getInstance(role, options = {}) ⇒ Object

GemFire is a Singleton. new() is hidden, so use getInstance() to both create the GemFire instance and to launch GemFire.

The required Hash is used to configure clients and servers.
For example, GemFire.getInstance('server', {'region-name'=> 'data', 'locators' => 'localhost:10355'})

Since it is a Singleton, successive calls to GemFire.getInstance() will return the single instance that was instantiated by the first call.



29
30
31
# File 'lib/gemfire-jruby.rb', line 29

def GemFire.getInstance(role, options={})
  self.instance ||= new(role, options)
end

Instance Method Details

#addListener(cacheListener) ⇒ Object

Add and remove CacheListeners to the cache region



139
140
141
# File 'lib/gemfire-jruby.rb', line 139

def addListener(cacheListener)
  @region.getAttributesMutator.addCacheListener(cacheListener)
end

#clearObject

Delete all entries (key=>value pairs) from the GemFire cache. Returns a JRuby Hash.



132
133
134
135
136
# File 'lib/gemfire-jruby.rb', line 132

def clear
  @region.clear
rescue CacheException => e
  logger.error("GemfireCache Error (#{e}): #{e.message}")
end

#create(key, value) ⇒ Object

GemFire api



60
61
62
63
64
# File 'lib/gemfire-jruby.rb', line 60

def create(key, value)
  @region.create(key.to_yaml, value.to_yaml)
rescue CacheException => e
  logger.error("GemfireCache Error (#{e}): #{e.message}")
end

#decrement(key) ⇒ Object

Not implemented by GemFire. Raises an exception when called.



171
172
173
# File 'lib/gemfire-jruby.rb', line 171

def decrement(key)
  raise "Not supported by Gemfire"
end

#delete(key) ⇒ Object

Alias for destroy(key) … for compatibility with memcached



102
103
104
105
106
107
# File 'lib/gemfire-jruby.rb', line 102

def delete(key)
  super
  @region.destroy(key.to_yaml)
rescue CacheException => e
  logger.error("GemfireCache Error (#{e}): #{e.message}")
end

#delete_matched(matcher) ⇒ Object

Not implemented by GemFire. Raises an exception when called.



176
177
178
# File 'lib/gemfire-jruby.rb', line 176

def delete_matched(matcher)
  raise "Not supported by Gemfire"
end

#destroy(key) ⇒ Object

Destroy the entry stored in the GemFire cache at key. key can be any JRuby object. Returns the value that was deleted.



79
80
81
82
83
# File 'lib/gemfire-jruby.rb', line 79

def destroy(key)
  @region.destroy(key.to_yaml)
rescue CacheException => e
  logger.error("GemfireCache Error (#{e}): #{e.message}")
end

#exist?(key) ⇒ Boolean

Check if there is an entry accessible by key in the GemFire cache. Returns a boolean.

Returns:

  • (Boolean)


123
124
125
126
127
128
129
# File 'lib/gemfire-jruby.rb', line 123

def exist?(key)
  if @region.getAttributes.getPoolName then
    @region.containsKeyOnServer(key.to_yaml)
  else
    @region.containsKey(key.to_yaml)
  end
end

#increment(key) ⇒ Object

Not implemented by GemFire. Raises an exception when called.



166
167
168
# File 'lib/gemfire-jruby.rb', line 166

def increment(key)
  raise "Not supported by Gemfire"
end

#invalidate(key) ⇒ Object



72
73
74
75
76
# File 'lib/gemfire-jruby.rb', line 72

def invalidate(key)
  @region.invalidate(key.to_yaml)
rescue CacheException => e
  logger.error("GemfireCache Error (#{e}): #{e.message}")
end

#keys(onServer = true) ⇒ Object

Fetch all of the keys. Optional argument controls whether the keys come from the server orReturns a JRuby Array of JRuby objects.



110
111
112
113
114
115
116
117
118
119
120
# File 'lib/gemfire-jruby.rb', line 110

def keys(onServer=true)
  keySet = nil
  result = []
  if (onServer && (@role == 'client')) then
    keySet = @region.keySetOnServer
  else
    keySet = @region.keySet
  end
  keySet.each do |k| result << YAML::load(k) end
  result
end

#put(key, value) ⇒ Object



66
67
68
69
70
# File 'lib/gemfire-jruby.rb', line 66

def put(key, value)
  @region.put(key.to_yaml, value.to_yaml)
rescue CacheException => e
  logger.error("GemfireCache Error (#{e}): #{e.message}")
end

#read(key) ⇒ Object

Read a value from the GemFire cache. key can be any JRuby object. Returns the value stored at key.



86
87
88
89
90
91
# File 'lib/gemfire-jruby.rb', line 86

def read(key)
  super
  YAML::load(@region.get(key.to_yaml))
rescue CacheException => e
    logger.error("GemfireCache Error (#{e}): #{e.message}")
end

#removeListener(cacheListener) ⇒ Object



143
144
145
# File 'lib/gemfire-jruby.rb', line 143

def removeListener(cacheListener)
  @region.getAttributesMutator.removeCacheListener(cacheListener)
end

#setLoader(cacheLoader) ⇒ Object

Install a CacheLoader into the cache region



157
158
159
160
161
162
163
# File 'lib/gemfire-jruby.rb', line 157

def setLoader(cacheLoader)
  if @role == 'server' then
    @region.getAttributesMutator.setCacheLoader(cacheLoader)
  else
    raise 'Only servers can have CacheLoaders'
  end
end

#setWriter(cacheWriter) ⇒ Object

Install a CacheWriter into the client’s cache region



148
149
150
151
152
153
154
# File 'lib/gemfire-jruby.rb', line 148

def setWriter(cacheWriter)
  if @role == 'server' then
    @region.getAttributesMutator.setCacheWriter(cacheWriter)
  else
    raise 'Only servers can have CacheWriters'
  end
end

#write(key, value) ⇒ Object

Alias for put(key, value) … for compatibility with memcached



94
95
96
97
98
99
# File 'lib/gemfire-jruby.rb', line 94

def write(key, value)
  super
  @region.put(key.to_yaml, value.to_yaml)
rescue CacheException => e
  logger.error("GemfireCache Error (#{e}): #{e.message}")
end