1. Go to this page and download the library: Download vaibhavpandeyvpz/godam library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
vaibhavpandeyvpz / godam example snippets
use Godam\Store\MemoryStore;
$store = new MemoryStore();
use Godam\Store\FileSystemStore;
$store = new FileSystemStore('/path/to/cache/directory');
use Godam\Store\RedisStore;
$redis = new Redis();
$redis->connect('localhost', 6379);
$store = new RedisStore($redis);
use Godam\Store\PredisStore;
use Predis\Client;
$redis = new Client('tcp://127.0.0.1:6379');
$store = new PredisStore($redis);
use Godam\Store\MemcacheStore;
$memcache = new Memcache();
$memcache->connect('localhost', 11211);
$store = new MemcacheStore($memcache);
use Godam\Cache;
use Godam\Store\MemoryStore;
$cache = new Cache(new MemoryStore());
// Store a value with TTL (time to live in seconds)
$cache->set('user:123', ['name' => 'John', 'email' => '[email protected]'], 3600);
// Retrieve a value
$user = $cache->get('user:123');
// Get with default value if key doesn't exist
$user = $cache->get('user:456', ['name' => 'Guest']);
// Check if a key exists
$exists = $cache->has('user:123');
// Delete a single key
$cache->delete('user:123');
// Delete multiple keys
$cache->deleteMultiple(['user:123', 'user:456']);
// Store multiple values at once
$cache->setMultiple([
'key1' => 'value1',
'key2' => 'value2',
], 3600);
// Get multiple values at once
$values = $cache->getMultiple(['key1', 'key2'], []);
// Clear all cache
$cache->clear();
use Godam\CacheItemPool;
use Godam\Store\FileSystemStore;
$pool = new CacheItemPool(new FileSystemStore(__DIR__ . '/cache'));
// Get a cache item
$item = $pool->getItem('user:123');
if ($item->isHit()) {
// Cache hit - item exists and is not expired
$user = $item->get();
} else {
// Cache miss - set the value
$item->set(['name' => 'John', 'email' => '[email protected]']);
// Set expiration time (in seconds)
$item->expiresAfter(3600);
// Or set expiration to a specific date/time
// $item->expiresAt(new \DateTime('+1 hour'));
// Save the item
$pool->save($item);
}
// Get multiple items at once
$items = $pool->getItems(['user:123', 'user:456']);
// Save a deferred item (will be saved on commit)
$item = $pool->getItem('user:789');
$item->set(['name' => 'Jane']);
$item->expiresAfter(1800);
$pool->saveDeferred($item);
// Commit all deferred items
$pool->commit();
// Delete an item
$pool->deleteItem('user:123');
// Delete multiple items
$pool->deleteItems(['user:123', 'user:456']);
// Clear all items
$pool->clear();
use Godam\Cache;
use Godam\Store\MemoryStore;
$cache = new Cache(new MemoryStore());
// Cache for 1 hour (3600 seconds)
$cache->set('key1', 'value1', 3600);
// Cache forever (no expiration)
$cache->set('key2', 'value2', null);
// Cache for 30 minutes
$cache->set('key3', 'value3', 1800);
// Using DateInterval
$cache->set('key4', 'value4', new \DateInterval('PT1H')); // 1 hour
use Godam\StoreInterface;
class CustomStore implements StoreInterface
{
public function get(string $key): mixed { /* ... */ }
public function set(string $key, mixed $value, ?int $ttl = null): bool { /* ... */ }
public function delete(string $key): bool { /* ... */ }
public function clear(): bool { /* ... */ }
public function has(string $key): bool { /* ... */ }
}