-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy path01-cache-factory.php
More file actions
129 lines (109 loc) · 4.09 KB
/
01-cache-factory.php
File metadata and controls
129 lines (109 loc) · 4.09 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<?php
declare(strict_types=1);
/**
* Example: Using CacheFactory for Optional Cache Adapters.
*
* Demonstrates how to use CacheFactory to create cache adapters
* from optional dependencies (symfony/cache, ext-apcu, ext-redis).
*/
require_once __DIR__ . '/../../vendor/autoload.php';
require_once __DIR__ . '/../helpers.php';
use tommyknocker\pdodb\cache\CacheFactory;
use tommyknocker\pdodb\PdoDb;
echo "=== CacheFactory Examples ===\n\n";
// Get database configuration
$config = getExampleConfig();
$driver = $config['driver'] ?? 'sqlite';
echo "1. Filesystem Cache (requires symfony/cache):\n";
$fsConfig = [
'type' => 'filesystem',
'directory' => sys_get_temp_dir() . '/pdodb_cache_example',
'namespace' => 'example',
'default_lifetime' => 3600,
];
$fsCache = CacheFactory::create($fsConfig);
if ($fsCache !== null) {
echo " ✓ Filesystem cache created\n";
$fsCache->set('test_key', 'test_value', 60);
echo ' ✓ Cache set/get works: ' . ($fsCache->get('test_key') === 'test_value' ? 'OK' : 'FAIL') . "\n";
$fsCache->clear();
} else {
echo " ✗ Filesystem cache not available (symfony/cache not installed)\n";
}
echo "\n2. APCu Cache (requires ext-apcu and symfony/cache):\n";
if (extension_loaded('apcu') && function_exists('apcu_enabled') && apcu_enabled()) {
$apcuConfig = [
'type' => 'apcu',
'namespace' => 'pdodb_example',
];
$apcuCache = CacheFactory::create($apcuConfig);
if ($apcuCache !== null) {
echo " ✓ APCu cache created\n";
$apcuCache->set('test_key', 'test_value', 60);
echo ' ✓ Cache set/get works: ' . ($apcuCache->get('test_key') === 'test_value' ? 'OK' : 'FAIL') . "\n";
$apcuCache->delete('test_key');
} else {
echo " ✗ APCu cache not available (symfony/cache not installed)\n";
}
} else {
echo " ✗ APCu not available (ext-apcu not installed or not enabled)\n";
}
echo "\n3. Redis Cache (requires ext-redis and symfony/cache):\n";
if (extension_loaded('redis')) {
$redisConfig = [
'type' => 'redis',
'host' => '127.0.0.1',
'port' => 6379,
'database' => 15, // Use database 15 for testing
'namespace' => 'pdodb_example',
];
$redisCache = CacheFactory::create($redisConfig);
if ($redisCache !== null) {
echo " ✓ Redis cache created\n";
$redisCache->set('test_key', 'test_value', 60);
echo ' ✓ Cache set/get works: ' . ($redisCache->get('test_key') === 'test_value' ? 'OK' : 'FAIL') . "\n";
$redisCache->clear();
} else {
echo " ✗ Redis cache not available (ext-redis not installed, symfony/cache not installed, or Redis server not running)\n";
}
} else {
echo " ✗ Redis not available (ext-redis not installed)\n";
}
echo "\n4. Using Cache with PdoDb:\n";
$cacheConfig = [
'type' => 'filesystem',
'directory' => sys_get_temp_dir() . '/pdodb_cache_example',
];
$cache = CacheFactory::create($cacheConfig);
if ($cache !== null) {
try {
$db = new PdoDb($driver, $config, [], null, $cache);
} catch (\Throwable $e) {
echo "⚠️ Connection failed: {$e->getMessage()}\n";
echo " (Check your database server and config settings)\n";
exit(1);
}
echo " ✓ PdoDb created with cache\n";
// Setup test table
$schema = $db->schema();
$schema->dropTableIfExists('cache_test');
$schema->createTable('cache_test', [
'id' => $schema->primaryKey(),
'name' => $schema->string(100),
'created_at' => $schema->timestamp(),
'updated_at' => $schema->timestamp(),
]);
// Insert test data
$db->find()->from('cache_test')->insert(['name' => 'Test User']);
// Query with caching
$users1 = $db->find()->from('cache_test')->get();
echo " ✓ First query executed (may be cached)\n";
$users2 = $db->find()->from('cache_test')->get();
echo " ✓ Second query executed (may use cache)\n";
// Cleanup
$schema->dropTableIfExists('cache_test');
$cache->clear();
} else {
echo " ✗ Cache not available\n";
}
echo "\n✓ Examples completed\n";