root/dotfiles/vim/from_kyushu/.vim/eclim/autoload/eclim/cache.vim @ 34565

Revision 34565, 3.2 kB (checked in by from_kyushu, 5 years ago)

install eclim

Line 
1" Author:  Eric Van Dewoestine
2"
3" Description: {{{
4"   Caching functionality
5"
6" License:
7"
8" Copyright (C) 2005 - 2009  Eric Van Dewoestine
9"
10" This program is free software: you can redistribute it and/or modify
11" it under the terms of the GNU General Public License as published by
12" the Free Software Foundation, either version 3 of the License, or
13" (at your option) any later version.
14"
15" This program is distributed in the hope that it will be useful,
16" but WITHOUT ANY WARRANTY; without even the implied warranty of
17" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18" GNU General Public License for more details.
19"
20" You should have received a copy of the GNU General Public License
21" along with this program.  If not, see <http://www.gnu.org/licenses/>.
22"
23" }}}
24
25" Global Variables {{{
26  if !exists('g:EclimCacheDir')
27    let g:EclimCacheDir = expand('~/.eclim/cache')
28  endif
29" }}}
30
31" Set(key, content, metadata) {{{
32" Adds the supplied content (list of lines) along with the supplied metadata
33" (dictionary of key / value pairs) to the cache under the specified key.
34function! eclim#cache#Set(key, content, ...)
35  if !s:InitCache()
36    return
37  endif
38
39  call eclim#cache#Delete(a:key)
40
41  let file = s:GetCachedFilename(a:key)
42  let content = a:content
43  if a:0 > 0 && len(a:1) > 0
44    let content = [string(a:1)] + content
45  else
46    let content = [''] + content
47  endif
48  call writefile(content, file)
49
50  if executable('gzip')
51    call eclim#util#System('gzip "' . file . '"')
52  endif
53endfunction " }}}
54
55" Get(key [, valid]) {{{
56" Gets the content stored under the specified key.  An optional 'valid'
57" argument may be supplied which must be a FuncRef which will be passed the
58" metadata and must determine if the " cached value is still valid by
59" returning 1 for valid and 0 for invalid.
60" Returns a dictionary containing keys 'metadata' and 'content' or an empty
61" dictionary if no valid cache value found.
62function! eclim#cache#Get(key, ...)
63  if !s:InitCache()
64    return
65  endif
66
67  let file = s:GetCachedFilename(a:key)
68  if filereadable(file . '.gz')
69    call eclim#util#System('gzip -d "' . file . '.gz"')
70    let contents = readfile(file)
71    call eclim#util#System('gzip "' . file . '"')
72  elseif filereadable(file)
73    let contents = readfile(file)
74  else
75    return {}
76  endif
77
78  let metadata = contents[0] != '' ? eval(contents[0]) : {}
79  if len(a:000) > 0
80    let Function = a:000[0]
81    if !Function(metadata)
82      call eclim#cache#Delete(a:key)
83      return {}
84    endif
85  endif
86
87  return {'metadata': metadata, 'content': contents[1:]}
88endfunction " }}}
89
90" Delete(key) {{{
91" Delete any cached content under the specified key.
92function! eclim#cache#Delete(key)
93  if !s:InitCache()
94    return
95  endif
96
97  let file = s:GetCachedFilename(a:key)
98  if filereadable(file . '.gz')
99    call delete(file . '.gz')
100  endif
101
102  if filereadable(file)
103    call delete(file)
104  endif
105endfunction " }}}
106
107" s:GetCachedFilename(key) {{{
108function! s:GetCachedFilename(key)
109  return g:EclimCacheDir . '/' . substitute(a:key, '\W\+', '_', 'g') . '.cache'
110endfunction " }}}
111
112" s:InitCache() {{{
113" Initializes the cache.
114function! s:InitCache()
115  if !isdirectory(g:EclimCacheDir)
116    call mkdir(g:EclimCacheDir, 'p')
117  endif
118
119  return 1
120endfunction " }}}
121
122" vim:ft=vim:fdm=marker
Note: See TracBrowser for help on using the browser.