yaocaigen / httplib2

Automatically exported from code.google.com/p/httplib2
0 stars 0 forks source link

cache: concurrent updates #245

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
One suggestion: One 'safe' way of updating the same cached file with multiple 
threads is to write the file under a random, temporary name and then rename it. 
Rename operations should be atomic. 

I would suggest the following change in httplib2/__init__.py

class FileCache(object):
...
    def set(self, key, value):
        cacheFullPath = os.path.join(self.cache, self.safe(key))
        fd, fn = tempfile.mkstemp(dir=self.cache)
        f = os.fdopen(fd, 'wb')
        f.write(value)
        f.close()
        shutil.move(fn, cacheFullPath)

I attached the modified file.

Original issue reported on code.google.com by vgaro...@gmail.com on 7 Feb 2013 at 8:29

Attachments: