Class: Pione::System::FileCache::SimpleCacheMethod

Inherits:
FileCacheMethod show all
Defined in:
lib/pione/system/file-cache.rb

Overview

SimpleCacheMethod is a simple cache method implementation.

Instance Method Summary collapse

Constructor Details

#initializeSimpleCacheMethod

Creates a method.



82
83
84
85
# File 'lib/pione/system/file-cache.rb', line 82

def initialize
  @table = {}
  @tmpdir = Global.file_cache_directory
end

Instance Method Details

#get(uri) ⇒ String

Gets cached data path from the uri resource.

Parameters:

  • resource uri

Returns:

  • cached path



92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/pione/system/file-cache.rb', line 92

def get(uri)
  # check cached or not
  unless @table.has_key?(uri)
    # prepare cache path
    path = prepare_cache_path

    # link the resource file to cache path
    Resource[uri].link_to(path)
    @table[uri.to_s] = path
  end

  return @table[uri.to_s]
end

#put(src, uri) ⇒ void

This method returns an undefined value.

Puts the data to uri resource and caches it in local.



108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/pione/system/file-cache.rb', line 108

def put(src, uri)
  # prepare cache path
  path = prepare_cache_path

  # move the file from the working directory to cache
  FileUtils.mv(src, path)

  # make a symbolic link from original location to the cache
  FileUtils.symlink(path, src)

  # copy from cache to the resource file
  @table[uri.to_s] = path
  Resource[uri].link_from(path)
end

#shift(old_uri, new_uri) ⇒ void

This method returns an undefined value.

Parameters:

  • old resource uri

  • new resource uri



128
129
130
131
132
133
134
135
# File 'lib/pione/system/file-cache.rb', line 128

def shift(old_uri, new_uri)
  if path = @table[old_uri.to_s]
    if new_uri.scheme == "local"
      FileUtils.symlink(new_uri.path, path, :force => true)
    end
    @table[new_uri.to_s] = path
  end
end