Client
1 year ago
.htaccess
1 year ago
Box.php
1 year ago
Cache.php
1 year ago
ChunkedDownload.php
1 year ago
ChunkedUpload.php
1 year ago
DirIterator.php
1 year ago
index.html
1 year ago
web.config
1 year ago
Cache.php
148 lines
| 1 | <?php |
| 2 | /* |
| 3 | * |
| 4 | * JetBackup @ package |
| 5 | * Created By Idan Ben-Ezra |
| 6 | * |
| 7 | * Copyrights @ JetApps |
| 8 | * https://www.jetapps.com |
| 9 | * |
| 10 | **/ |
| 11 | namespace JetBackup\Destination\Vendors\Box; |
| 12 | |
| 13 | defined( '__JETBACKUP__' ) or die( 'Restricted access' ); |
| 14 | |
| 15 | class Cache { |
| 16 | |
| 17 | const CACHE_DEFAULT_SIZE = 500; |
| 18 | const CACHE_DEFAULT_TOLERANCE = 100; |
| 19 | |
| 20 | private array $_hash; |
| 21 | private array $_priorities; |
| 22 | private int $_size; |
| 23 | private int $_tolerance; |
| 24 | |
| 25 | /** |
| 26 | * |
| 27 | */ |
| 28 | public function __construct(){ |
| 29 | $this->_hash = []; |
| 30 | $this->_priorities = []; |
| 31 | $this->_size = self::CACHE_DEFAULT_SIZE; |
| 32 | $this->_tolerance = self::CACHE_DEFAULT_TOLERANCE; |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * @param int $size |
| 37 | * |
| 38 | * @return void |
| 39 | */ |
| 40 | public function setSize(int $size):void { $this->_size = $size; } |
| 41 | |
| 42 | /** |
| 43 | * @return int |
| 44 | */ |
| 45 | public function getSize():int { return $this->_size; } |
| 46 | |
| 47 | /** |
| 48 | * @param int $tolerance |
| 49 | * |
| 50 | * @return void |
| 51 | */ |
| 52 | public function setTolerance(int $tolerance):void { $this->_tolerance = $tolerance; } |
| 53 | |
| 54 | /** |
| 55 | * @return int |
| 56 | */ |
| 57 | public function getTolerance():int { return $this->_tolerance; } |
| 58 | |
| 59 | /** |
| 60 | * @param string $key |
| 61 | * |
| 62 | * @return string|null |
| 63 | */ |
| 64 | public function get(string $key):?string { |
| 65 | if(!isset($this->_hash[$key])) return null; |
| 66 | $this->_promote($key); |
| 67 | return $this->_hash[$key]; |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * @param string $key |
| 72 | * @param mixed $data |
| 73 | * |
| 74 | * @return void |
| 75 | */ |
| 76 | public function set(string $key, $data):void { |
| 77 | if($this->has($key)) $this->remove($key); |
| 78 | $this->_hash[$key] = $data; |
| 79 | $this->_priorities[] = $key; |
| 80 | $this->_cleanup(); |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * @return array |
| 85 | */ |
| 86 | public function getList():array { |
| 87 | return $this->_hash; |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * @param string $key |
| 92 | * |
| 93 | * @return bool |
| 94 | */ |
| 95 | public function has(string $key):bool { |
| 96 | return isset($this->_hash[$key]); |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * @param string $key |
| 101 | * |
| 102 | * @return void |
| 103 | */ |
| 104 | public function remove(string $key):void { |
| 105 | if(!$this->has($key)) return; |
| 106 | $this->_removePriority($key); |
| 107 | unset($this->_hash[$key]); |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * @return int |
| 112 | */ |
| 113 | public function getActualSize():int { |
| 114 | return sizeof($this->_priorities); |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * @param string $key |
| 119 | * |
| 120 | * @return void |
| 121 | */ |
| 122 | private function _promote(string $key):void { |
| 123 | $this->_removePriority($key); |
| 124 | $this->_priorities[] = $key; |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * @param string $key |
| 129 | * |
| 130 | * @return void |
| 131 | */ |
| 132 | private function _removePriority(string $key):void { |
| 133 | $ind = array_search($key, $this->_priorities); |
| 134 | array_splice($this->_priorities, $ind, 1); |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * @return void |
| 139 | */ |
| 140 | private function _cleanup():void { |
| 141 | if($this->getActualSize() <= $this->getSize()+$this->getTolerance()) return; |
| 142 | while($this->getActualSize() > $this->getSize()) { |
| 143 | $key = array_shift($this->_priorities); |
| 144 | unset($this->_hash[$key]); |
| 145 | } |
| 146 | } |
| 147 | } |
| 148 |