PluginProbe ʕ •ᴥ•ʔ
JetBackup – Backup, Restore & Migrate / trunk
JetBackup – Backup, Restore & Migrate vtrunk
3.1.22.3 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.4.8 1.4.8.1 1.4.9 1.5.0 1.5.1 1.5.1.1 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.5.8 1.6.0 1.6.10 1.6.11 1.6.12 1.6.13 1.6.15 1.6.5.1 1.6.8.8 1.6.9 1.6.9.1 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7.5 2.0.8.7 2.0.9.11 2.0.9.14 2.0.9.15 2.0.9.6 2.0.9.7 2.0.9.9 3.1.10.7 3.1.11.1 3.1.12.3 3.1.13.4 3.1.14.17 3.1.15.4 3.1.16.1 3.1.17.5 3.1.18.10 3.1.18.8 3.1.18.9 3.1.19.8 3.1.20.3 3.1.21.3 3.1.7.9 3.1.9.2 trunk 1.1.90 1.1.91 1.2.0 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.6 1.3.7 1.3.8 1.3.9 1.4.0 1.4.1 1.4.2
backup / src / JetBackup / Destination / Vendors / GoogleDrive / Cache.php
backup / src / JetBackup / Destination / Vendors / GoogleDrive Last commit date
Client 1 year ago .htaccess 1 year ago Cache.php 1 year ago ChunkedDownload.php 1 year ago ChunkedUpload.php 1 year ago DirIterator.php 1 year ago GoogleDrive.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\GoogleDrive;
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