NovaBryan/EVE_Dump
Log | Files | 📦 下载最新源码 (.zip) | Refs | README

disk_storage.py (2832B)


      1 #Embedded file name: C:\BuildAgent\work\20d73f1b24eed06e\eve\release\V24.01\packages\caching\client\storage\disk_storage.py
      2 import errno
      3 import os
      4 import json
      5 import uthread2
      6 from caching.client.storage.base_storage import BaseStorage
      7 from caching.client.storage.disk_utils import _get_data_path
      8 from locks import FifoLock
      9 import logging
     10 logger = logging.getLogger('caching')
     11 
     12 class JsonDiskStorage(BaseStorage):
     13 
     14     def get(self):
     15         path = _get_data_path(self.content_id, file_type='.json')
     16         if not os.path.exists(path):
     17             return
     18         with open(path, 'r') as f:
     19             data = json.load(f)
     20             return data
     21 
     22     def set(self, value):
     23         path = _get_data_path(self.content_id, file_type='.json')
     24         if not os.path.exists(path):
     25             new_file = file(path, 'w')
     26             new_file.close()
     27         with open(path, 'w') as f:
     28             json.dump(value, f)
     29 
     30 
     31 class BinaryDiskStorage(BaseStorage):
     32     RETRY_DELAY = 0.1
     33 
     34     def __init__(self, content_id, retry_attempts = 5):
     35         super(BinaryDiskStorage, self).__init__(content_id)
     36         self._access_lock = FifoLock()
     37         self._retry_attempts = retry_attempts
     38 
     39     def _retry_wrapper(self, impl_method, *args, **kwargs):
     40         with self._access_lock:
     41             for attempt in range(self._retry_attempts):
     42                 try:
     43                     return impl_method(*args, **kwargs)
     44                 except (OSError, IOError) as e:
     45                     if attempt < self._retry_attempts - 1:
     46                         uthread2.Sleep(BinaryDiskStorage.RETRY_DELAY)
     47                     else:
     48                         raise
     49 
     50     def get(self):
     51         return self._retry_wrapper(self.get_impl)
     52 
     53     def get_impl(self):
     54         path = _get_data_path(self.content_id, file_type='.bin')
     55         if not os.path.exists(path):
     56             return
     57         with open(path, 'rb') as f:
     58             data = f.read()
     59             return data
     60 
     61     def set(self, value):
     62         self._retry_wrapper(self._set_impl, value)
     63 
     64     def _set_impl(self, value):
     65         path = _get_data_path(self.content_id, file_type='.bin')
     66         if not os.path.exists(path):
     67             new_file = file(path, 'wb')
     68             new_file.close()
     69         with open(_get_data_path(self.content_id, file_type='.bin'), 'wb') as f:
     70             f.write(value)
     71             f.flush()
     72             os.fsync(f.fileno())
     73 
     74     def clear(self):
     75         self._retry_wrapper(self._clear_impl)
     76 
     77     def _clear_impl(self):
     78         data_path = _get_data_path(self.content_id, file_type='.bin')
     79         if os.path.exists(data_path):
     80             try:
     81                 os.remove(data_path)
     82             except IOError as e:
     83                 if e.errno != errno.ENOENT:
     84                     raise
     85                 logger.warning('Tried to remove %s but it no longer exists', data_path)