Download Files with zagoload - part 4

Caching

You proxy parameter define how to handle cached files

  • CacheMode.Enabled - check in cache for the source file. if it does not exist download it.
  • CacheMode.InCache - check in cache for the source file. if it does not exist report that failure.
  • CacheMode.Disabled - download the source file to cache even if it exist in the cache.
sample_04.py
 import zagoload
 import os
 import time
 def download(source,cacheMode, cacheTime=0):
   def info(ss):
     import sys ;sys.stdout.write(ss + u'\n')
   ff = zagoload.load(source,cacheMode=cacheMode,cacheTime=cacheTime)
   if ff.valid:
     info( u'{0} => {1}. Time : {2:.2f}'.format(ff.source, ff.target, os.path.getmtime(ff.target) % 10000) )
   else:
     info( 'Failed to download {0}'.format(ff.source) )
 # will fail since no file in cache
 download( 'http://www.google.com', zagoload.CacheMode.InCache     )

 # will download from source
 download( 'http://www.google.com', zagoload.CacheMode.Enabled     )

 # will use cached file
 download( 'http://www.google.com', zagoload.CacheMode.Enabled     )

 # will use cached file
 download( 'http://www.google.com', zagoload.CacheMode.InCache     )

 # will download the file from source, even though the file exists in the cache
 download( 'http://www.google.com', zagoload.CacheMode.Disabled    )

 # will use cached file
 download( 'http://www.google.com', zagoload.CacheMode.Enabled     )

 # will download the file from source, since the cached file is older than 5 seconds
 time.sleep(10)
 download( 'http://www.google.com', zagoload.CacheMode.Enabled  , 5)
sample_04.py output
 # python sample_04.py
 http://www.google.com => Cache/fW/file_fWebvNyPE1OGX2RS.bin. Time : 8774.76
 http://www.google.com => Cache/fW/file_fWebvNyPE1OGX2RS.bin. Time : 8774.76
 http://www.google.com => Cache/fW/file_fWebvNyPE1OGX2RS.bin. Time : 8774.76
 http://www.google.com => Cache/fW/file_fWebvNyPE1OGX2RS.bin. Time : 8774.76
 http://www.google.com => Cache/fW/file_fWebvNyPE1OGX2RS.bin. Time : 9232.80
 http://www.google.com => Cache/fW/file_fWebvNyPE1OGX2RS.bin. Time : 9232.80
 http://www.google.com => Cache/fW/file_fWebvNyPE1OGX2RS.bin. Time : 9243.05