Class: KeepYourHead::Cache

Inherits:
Object
  • Object
show all
Defined in:
lib/Keepyourhead/Cache.rb

Constant Summary collapse

CachePath =
Resources::user("cache/")
Filename =
::File.expand_path( "index.yaml", CachePath )
MaxAge =

60 days

2 * 30 * 24 * 60 * 60
MaxSize =

25 mb

25 * 1024 * 1024

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCache

Returns a new instance of Cache.



61
62
63
64
# File 'lib/Keepyourhead/Cache.rb', line 61

def initialize
	@monitor = Monitor.new
	load
end

Instance Attribute Details

#cacheSizeObject

Returns the value of attribute cacheSize.



59
60
61
# File 'lib/Keepyourhead/Cache.rb', line 59

def cacheSize
  @cacheSize
end

Instance Method Details

#addCompilation(compilation) ⇒ Object



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/Keepyourhead/Cache.rb', line 150

def addCompilation( compilation )
	if compilation.success then
		filenames = compilation.filenames.map { |filename| fnew = createNewFilename; ::FileUtils.cp(filename, fnew); fnew }

		item = CacheItem.new self, true, filenames, Time.new
	else
		item = CacheItem.new self, false, [], Time.new
	end

	lock {
		@cache[compilation.content] = item
		self.cacheSize += item.size if self.cacheSize
	}

	checkSize
end

#getCacheItem(flashcard, type, compileOptions = {}, &block) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
# File 'lib/Keepyourhead/Cache.rb', line 123

def getCacheItem( flashcard, type, compileOptions = {}, &block)
	width ||= Gdk::Screen.default.width
	if not hasCacheItem( flashcard, type, compileOptions, &block ) then
		compilation = flashcard.compileImage( type, compileOptions )
		addCompilation( compilation )
		ret = hasCacheItem( flashcard, type, compileOptions, &block )
		compilation.remove

		assert ret
	end
end

#hasCacheItem(flashcard, type, compileOptions = {}, &block) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/Keepyourhead/Cache.rb', line 135

def hasCacheItem( flashcard, type, compileOptions = {}, &block )
	item = nil
	
	content = flashcard.content(type, compileOptions)
	lock {
		item = @cache[content]
	
		item.lastAccess = Time.new if item

		block.call(item) if block
	}

	not item == nil
end

#loadObject



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/Keepyourhead/Cache.rb', line 66

def load
	lock {
		::File::makedirs CachePath unless ::File.exist? CachePath

		@cache = {}
		begin
			data = []
			File.open( Filename, "r") {|file|
				data = YAML.load( file )
			}

#				pp data.inspect
			data.each { |key, success, filenames,lastAccess|
				item = CacheItem.new self, success, filenames || [], Time.parse(lastAccess)
				if item and item.ok? then
					@cache[key] = item
				end
			}

#				print "Cache loaded #{@cache.length} items"
		rescue Errno::ENOENT
			@cache = {}
			print "Cache not found - recreating"
		end

		cleanUp
		
		checkSize
	}
end

#writeBackObject



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/Keepyourhead/Cache.rb', line 97

def writeBack
	lock {
		::File::makedirs CachePath unless ::File.exist? CachePath

		cleanUp
		removeOld
		checkSize
		
		::File.mkdirs CachePath unless ::File.directory? CachePath

		::File.open( Filename, "w" ) { |file|
			data = []
			@cache.each { |key,item| 
				data << [key, item.success, item.filenames, item.lastAccess.to_s] 
			}
			file << data.to_yaml
#				YAML::dump(data, file)
		}
	}
end