-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyHibernateCache.php
More file actions
executable file
·81 lines (62 loc) · 2.67 KB
/
MyHibernateCache.php
File metadata and controls
executable file
·81 lines (62 loc) · 2.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<?php
/*
* Copyright (c) 2025 Bloxtor (http://bloxtor.com) and Joao Pinto (http://jplpinto.com)
*
* Multi-licensed: BSD 3-Clause | Apache 2.0 | GNU LGPL v3 | HLNC License (http://bloxtor.com/LICENSE_HLNC.md)
* Choose one license that best fits your needs.
*
* Original PHP Spring Lib Repo: https://github.com/a19836/php-spring-lib/
* Original Bloxtor Repo: https://github.com/a19836/bloxtor
*
* YOU ARE NOT AUTHORIZED TO MODIFY OR REMOVE ANY PART OF THIS NOTICE!
*/
include_once get_lib("sqlmap.hibernate.IHibernateClientCacheLayer");
class MyHibernateCache implements IHibernateClientCacheLayer {
private $root_folder;
private $ttl;
public function __construct($root_folder, $ttl = 86400) {
$this->root_folder = rtrim($root_folder, "/");
$this->ttl = $ttl;
}
// Check if file exists and if TTL is still valid
public function isValid($module_id, $service_id, $parameters = false, $options = false) {
$file = $this->getCacheFilePath($module_id, $service_id, $parameters, $options);
if (!file_exists($file))
return false;
// Check TTL expiration
$file_mtime = filemtime($file);
return time() - $file_mtime <= $this->ttl;
}
// Get cached result
public function get($module_id, $service_id, $parameters = false, $options = false) {
$file = $this->getCacheFilePath($module_id, $service_id, $parameters, $options);
if (!file_exists($file))
return null;
$content = file_get_contents($file);
return unserialize($content);
}
// Save result in cache
public function check($module_id, $service_id, $parameters = false, $result = false, $options = false) {
$file = $this->getCacheFilePath($module_id, $service_id, $parameters, $options);
$dir = dirname($file);
if (!is_dir($dir))
mkdir($dir, 0777, true);
return file_put_contents($file, serialize($result)) !== false;
}
// Generate file path based on: module_id, service_id, parameters, options
private function getCacheFilePath($module_id, $service_id, $parameters = false, $options = false) {
// Normalize input to avoid collisions
$key_data = array(
"module_id" => $module_id,
"service_id" => $service_id,
"parameters" => $parameters,
"options" => $options
);
// Unique deterministic ID
$cache_id = hash("crc32b", serialize($key_data));
// You can also shard the cache for performance:
$subfolder = substr($cache_id, 0, 2);
return $this->root_folder . "/" . $subfolder . "/" . $cache_id . ".cache";
}
}
?>