Created
April 28, 2022 07:37
-
-
Save radityopw/0a8b9d09ab867a2f152c3c04b90898cd to your computer and use it in GitHub Desktop.
session handler using google cloud storage
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require_once 'vendor/autoload.php'; | |
use Google\Cloud\Storage\StorageClient; | |
class MySessionHandler implements SessionHandlerInterface{ | |
private $savePath; | |
private $storage; | |
private $bucket; | |
public function __construct(){ | |
$this->savePath = "session-dev"; | |
$this->storage = new StorageClient([ | |
'keyFilePath' => 'session_dev.json' | |
]); | |
$this->bucket = $this->storage->bucket($this->savePath); | |
$this->storage->registerStreamWrapper(); | |
} | |
public function open($savePath, $sessionName) { | |
// tidak diimplementasikan karena sudah spesifik ke bucket tertentu di GCP | |
return true; | |
} | |
public function close() { | |
return true; | |
} | |
public function read($id){ | |
return (string) @file_get_contents('gs://'.$this->savePath.'/'.$id); | |
} | |
public function write($id, $data){ | |
return file_put_contents('gs://'.$this->savePath.'/'.$id, $data) === false ? false : true; | |
} | |
public function destroy($id){ | |
$object = $this->bucket->object($id); | |
$object->delete(); | |
return true; | |
} | |
public function gc($maxlifetime){ | |
// tidak diimplementasikan , lebih baik menggunakan konfigurasi lifecycle di bucket pada gcp | |
return true; | |
} | |
} | |
$handler = new MySessionHandler(); | |
session_set_save_handler($handler, true); | |
session_start(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment