Module: Ocular::DSL::Cache

Included in:
RunContext, Event::DefinitionProxy
Defined in:
lib/ocular/dsl/cache.rb

Instance Method Summary collapse

Instance Method Details

#cache_get(key) ⇒ Object



25
26
27
28
29
30
31
32
33
# File 'lib/ocular/dsl/cache.rb', line 25

def cache_get(key)
	m = mysql()
	begin
 	ret = m.query("SELECT value FROM ocular_cache WHERE keyname = '#{m.escape(key)}' AND (expires IS NULL OR expires > UNIX_TIMESTAMP(NOW()))").first
 	return ret["value"]
 rescue
 	return nil
 end
end

#cache_set(key, value, ttl = nil) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/ocular/dsl/cache.rb', line 9

def cache_set(key, value, ttl=nil)
	m = mysql()
		if ttl
			ttl = Time.now.to_i + ttl.to_i
		end
	begin
 	m.query("REPLACE INTO ocular_cache VALUES('#{m.escape(key)}', '#{m.escape(value)}', #{ttl == nil ? "NULL" : ttl});")
 rescue Mysql2::Error => e
 	puts "Got #{e}, #{e.to_s}"
 	if e.to_s.include?("doesn't exist")
			m.query("CREATE TABLE IF NOT EXISTS ocular_cache (keyname varchar(100) PRIMARY KEY, value text, expires int(11) null)")
  	m.query("REPLACE INTO ocular_cache VALUES('#{m.escape(key)}', '#{m.escape(value)}', #{ttl == nil ? "NULL" : ttl});")
 	end
 end
end