Class: Abst::Cache

Inherits:
Object
  • Object
show all
Defined in:
lib/include/cache.rb

Direct Known Subclasses

SystemCache

Constant Summary collapse

PREFIX =
'abst_cache_'
CACHE_DIR =
CACHE_DIR

Class Method Summary collapse

Class Method Details

.[](cache_id) ⇒ Object



25
26
27
28
29
30
# File 'lib/include/cache.rb', line 25

def self.[](cache_id)
	path = get_path(cache_id)

	return nil unless FileTest.exist?(path)
	open(path) {|io| return Marshal.load(io)}
end

.[]=(cache_id, data) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/include/cache.rb', line 12

def self.[]=(cache_id, data)
	if nil == data
		delete(cache_id)
		return
	end

	path = get_path(cache_id)
	dir = File.dirname(path)

	mkdir(dir) unless FileTest.exist?(dir)
	open(path, "w") {|io| Marshal.dump(data, io)}
end

.clearObject

Raises:

  • (NotImplementedError)


38
39
40
# File 'lib/include/cache.rb', line 38

def self.clear
	raise NotImplementedError
end

.delete(cache_id) ⇒ Object



32
33
34
35
36
# File 'lib/include/cache.rb', line 32

def self.delete(cache_id)
	path = get_path(cache_id)

	File.delete(path) if FileTest.exist?(path)
end

.exist?(cache_id) ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/include/cache.rb', line 42

def self.exist?(cache_id)
	return FileTest.exist?(get_path(cache_id))
end

.get_path(cache_id) ⇒ Object



46
47
48
49
50
51
52
53
# File 'lib/include/cache.rb', line 46

def self.get_path(cache_id)
	cache_id = cache_id.to_s
	unless /^[a-z0-9_]+$/ =~ cache_id
		raise ArgumentError, "Invalid cache_id. (Only a-z 0-9 and _ are available)"
	end

	return self::CACHE_DIR + self::PREFIX + cache_id
end

.mkdir(dir) ⇒ Object



6
7
8
9
10
# File 'lib/include/cache.rb', line 6

def self.mkdir(dir)
	pdir = File.dirname(dir)
	mkdir(pdir) unless FileTest.exist?(pdir)
	Dir::mkdir(dir)
end