Class: WirisPlugin::CacheImpl

Inherits:
Object
  • Object
show all
Extended by:
CacheInterface
Includes:
Wiris
Defined in:
lib/com/wiris/plugin/impl/CacheImpl.rb

Direct Known Subclasses

CacheFormulaImpl

Constant Summary collapse

@@backwards_compat =
true

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from CacheInterface

Cache

Constructor Details

#initialize(conf) ⇒ CacheImpl

Returns a new instance of CacheImpl.



21
22
23
24
25
# File 'lib/com/wiris/plugin/impl/CacheImpl.rb', line 21

def initialize(conf)
    super()
    self.conf = conf
    self.cacheFolder = getAndCheckFolder(ConfigurationKeys::CACHE_FOLDER)
end

Instance Attribute Details

#cacheFolderObject

Returns the value of attribute cacheFolder.



13
14
15
# File 'lib/com/wiris/plugin/impl/CacheImpl.rb', line 13

def cacheFolder
  @cacheFolder
end

#confObject

Returns the value of attribute conf.



12
13
14
# File 'lib/com/wiris/plugin/impl/CacheImpl.rb', line 12

def conf
  @conf
end

Class Method Details

.backwards_compatObject



15
16
17
# File 'lib/com/wiris/plugin/impl/CacheImpl.rb', line 15

def self.backwards_compat
    @@backwards_compat
end

.backwards_compat=(backwards_compat) ⇒ Object



18
19
20
# File 'lib/com/wiris/plugin/impl/CacheImpl.rb', line 18

def self.backwards_compat=(backwards_compat)
    @@backwards_compat = backwards_compat
end

Instance Method Details

#delete(key) ⇒ Object



71
72
# File 'lib/com/wiris/plugin/impl/CacheImpl.rb', line 71

def delete(key)
end

#deleteAllObject



57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/com/wiris/plugin/impl/CacheImpl.rb', line 57

def deleteAll()
    formulaFolder = getAndCheckFolder(ConfigurationKeys::FORMULA_FOLDER)
    cacheFolder = getAndCheckFolder(ConfigurationKeys::CACHE_FOLDER)
    includes = Array.new()
    includes::push("svg")
    includes::push("png")
    includes::push("csv")
    includes::push("txt")
    if !(PropertiesTools::getProperty(self.conf,ConfigurationKeys::SAVE_MODE,"xml") == "image")
        includes::push("ini")
    end
    Store::deleteDirectory(formulaFolder,includes)
    Store::deleteDirectory(cacheFolder,includes)
end

#get(key) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/com/wiris/plugin/impl/CacheImpl.rb', line 36

def get(key)
    extension = Std::substr(key,key::indexOf("."),key::length() - key::indexOf("."))
    digest = Std::substr(key,0,key::indexOf(extension))
    store = self.getFileStore(@cacheFolder,digest,extension)
    if @@backwards_compat
        if !store::exists()
            oldstore = Store::newStore(((@cacheFolder + "/") + digest) + extension)
            if !oldstore::exists()
                return nil
            end
            parent = store::getParent()
            parent::mkdirs()
            oldstore::moveTo(store)
        end
    else 
        if !store::exists()
            return nil
        end
    end
    return store::readBinary()
end

#getAndCheckFolder(key) ⇒ Object



73
74
75
76
77
78
79
# File 'lib/com/wiris/plugin/impl/CacheImpl.rb', line 73

def getAndCheckFolder(key)
    folder = PropertiesTools::getProperty(self.conf,key)
    if (folder == nil) || (folder::trim()::length() == 0)
        raise Exception,"Missing configuration value: " + key
    end
    return folder
end

#getFileStore(dir, digest, extension) ⇒ Object



83
84
85
# File 'lib/com/wiris/plugin/impl/CacheImpl.rb', line 83

def getFileStore(dir, digest, extension)
    return getFileStoreWithParent(getFolderStore(dir,digest),digest,extension)
end

#getFileStoreWithParent(parent, digest, extension) ⇒ Object



80
81
82
# File 'lib/com/wiris/plugin/impl/CacheImpl.rb', line 80

def getFileStoreWithParent(parent, digest, extension)
    return Store::newStoreWithParent(parent,Std::substr(digest,4).to_s + extension)
end

#getFolderStore(dir, digest) ⇒ Object



86
87
88
# File 'lib/com/wiris/plugin/impl/CacheImpl.rb', line 86

def getFolderStore(dir, digest)
    return Store::newStore((((dir + "/") + Std::substr(digest,0,2).to_s) + "/") + Std::substr(digest,2,2).to_s)
end

#isFormulaFileName(name) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
# File 'lib/com/wiris/plugin/impl/CacheImpl.rb', line 110

def isFormulaFileName(name)
    i = name::indexOf(".")
    if i == -1
        return nil
    end
    digest = Std::substr(name,0,i)
    if digest::length() != 32
        return nil
    end
    return digest
end

#set(key, value) ⇒ Object



26
27
28
29
30
31
32
33
34
35
# File 'lib/com/wiris/plugin/impl/CacheImpl.rb', line 26

def set(key, value)
    extension = Std::substr(key,key::indexOf("."),key::length() - key::indexOf("."))
    digest = Std::substr(key,0,key::indexOf(extension))
    parent = getFolderStore(@cacheFolder,digest)
    parent::mkdirs()
    store = getFileStoreWithParent(parent,digest,extension)
    if !store::exists()
        store::writeBinary(value)
    end
end

#updateFoldersStructureObject



89
90
91
92
# File 'lib/com/wiris/plugin/impl/CacheImpl.rb', line 89

def updateFoldersStructure()
    updateFolderStructure(getAndCheckFolder(ConfigurationKeys::CACHE_FOLDER))
    updateFolderStructure(getAndCheckFolder(ConfigurationKeys::FORMULA_FOLDER))
end

#updateFolderStructure(dir) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/com/wiris/plugin/impl/CacheImpl.rb', line 93

def updateFolderStructure(dir)
    folder = Store::newStore(dir)
    files = folder::list()
    if files != nil
        for i in 0..files::length - 1
            digest = isFormulaFileName(files[i])
            if digest != nil
                newFolder = getFolderStore(dir,digest)
                newFolder::mkdirs()
                newFile = getFileStoreWithParent(newFolder,digest,Std::substr(files[i],files[i]::indexOf(".") + 1))
                file = Store::newStoreWithParent(folder,files[i])
                file::moveTo(newFile)
            end
            i+=1
        end
    end
end