Class: KeepYourHead::Cache
- Inherits:
-
Object
- Object
- KeepYourHead::Cache
- Defined in:
- lib/Keepyourhead/Cache.rb
Constant Summary collapse
- CachePath =
Resources::user("cache/")
- Filename =
::File.( "index.yaml", CachePath )
- MaxAge =
60 days
2 * 30 * 24 * 60 * 60
- MaxSize =
25 mb
25 * 1024 * 1024
Instance Attribute Summary collapse
-
#cacheSize ⇒ Object
Returns the value of attribute cacheSize.
Instance Method Summary collapse
- #addCompilation(compilation) ⇒ Object
- #getCacheItem(flashcard, type, compileOptions = {}, &block) ⇒ Object
- #hasCacheItem(flashcard, type, compileOptions = {}, &block) ⇒ Object
-
#initialize ⇒ Cache
constructor
A new instance of Cache.
- #load ⇒ Object
- #writeBack ⇒ Object
Constructor Details
#initialize ⇒ Cache
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
#cacheSize ⇒ Object
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
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/Keepyourhead/Cache.rb', line 146 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
119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/Keepyourhead/Cache.rb', line 119 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
131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/Keepyourhead/Cache.rb', line 131 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 |
#load ⇒ Object
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 |
# File 'lib/Keepyourhead/Cache.rb', line 66 def load lock { @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 |
#writeBack ⇒ Object
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/Keepyourhead/Cache.rb', line 95 def writeBack lock { 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 |