generated from medeirosinacio/basic-php-project-structure
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6041e56
commit 3671a96
Showing
17 changed files
with
488 additions
and
149 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
#!/usr/bin/env bash | ||
|
||
mongosh "mongodb://mongo/default?replicaSet=rs0&readPreference=primary" <<EOF | ||
db.records.insertOne({ | ||
transaction_id: "txn12345", | ||
user_id: "1", | ||
expires_at: new Date(Date.now() + 60 * 1000) // Define a expiração para 1 minuto a partir de agora | ||
}); | ||
EOF |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,59 +1,5 @@ | ||
#!/usr/bin/env bash | ||
|
||
set -eu | ||
|
||
# Set variables | ||
SDDB_USER=user | ||
SDDB_PASS=password | ||
SDDB_DATABASE=default | ||
|
||
# replicate set initiate | ||
echo "Checking mongo container..." | ||
until mongosh --host mongo --eval "print(\"waited for connection\")" | ||
do | ||
sleep 1 | ||
done | ||
|
||
echo "Initializing replicaset..." | ||
mongosh --host mongo <<EOF | ||
rs.initiate( | ||
{ | ||
_id: "rs0", | ||
version: 1, | ||
members: [ | ||
{ _id: 0, host: "mongo:27017"} | ||
] | ||
} | ||
) | ||
rs.status() | ||
mongosh "mongodb://mongo/default?replicaSet=rs0&readPreference=primary" <<EOF | ||
db.getCollectionNames().forEach(c => db.getCollection(c).drop()) | ||
EOF | ||
|
||
|
||
echo "Creating admin user: root@root/admin" | ||
mongosh --host mongo <<EOF | ||
db.getSiblingDB('admin').createUser( | ||
{ | ||
user: "root", | ||
pwd: "root", | ||
roles: [ { role: "root", db: "admin" } ] | ||
} | ||
) | ||
rs.status() | ||
EOF | ||
|
||
echo "Creating normal user: ${SDDB_USER}:${SDDB_PASS}/${SDDB_DATABASE}" | ||
mongosh --host mongo <<EOF | ||
use ${SDDB_DATABASE} | ||
db.createUser( | ||
{ | ||
user: "${SDDB_USER}", | ||
pwd: "${SDDB_PASS}", | ||
roles: [ { role: "dbOwner", db: "${SDDB_DATABASE}" } ] | ||
} | ||
) | ||
EOF | ||
|
||
echo "Confirm normal user account" | ||
echo "---------------------------------------" | ||
mongosh --eval 'rs.status()' "mongodb://${SDDB_USER}:${SDDB_PASS}@mongo:27017/${SDDB_DATABASE}" | ||
echo "---------------------------------------" |
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
This file was deleted.
Oops, something went wrong.
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
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
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
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
This file was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Medeirosinacio\MongoTtlIndexChangeStream\Factories; | ||
|
||
use InvalidArgumentException; | ||
use Medeirosinacio\MongoTtlIndexChangeStream\LocalCacheDriver; | ||
use Psr\SimpleCache\CacheInterface; | ||
|
||
final class CacheFactory | ||
{ | ||
public static function make(string $driver = 'local'): CacheInterface | ||
{ | ||
return match ($driver) { | ||
'local' => new LocalCacheDriver(), | ||
default => throw new InvalidArgumentException('Invalid cache driver'), | ||
}; | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Medeirosinacio\MongoTtlIndexChangeStream\Factories; | ||
|
||
use MongoDB\Client; | ||
|
||
final class MongoFactory | ||
{ | ||
public static function make(): Client | ||
{ | ||
return new Client( | ||
'mongodb://mongo:27017', | ||
[ | ||
'username' => 'root', | ||
'password' => 'root', | ||
'ssl' => false, | ||
'replicaSet' => 'rs0', | ||
] | ||
); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Medeirosinacio\MongoTtlIndexChangeStream; | ||
|
||
use DateInterval; | ||
use Exception; | ||
use Psr\SimpleCache\CacheInterface; | ||
|
||
final class LocalCacheDriver implements CacheInterface | ||
{ | ||
private readonly string $cacheDir; | ||
|
||
public function __construct(string $cacheDir = '/app/runtime/cache') | ||
{ | ||
$this->cacheDir = rtrim($cacheDir, DIRECTORY_SEPARATOR); | ||
if (is_dir($this->cacheDir)) { | ||
return; | ||
} | ||
if (mkdir($this->cacheDir, 0755, true)) { | ||
return; | ||
} | ||
if (is_dir($this->cacheDir)) { | ||
return; | ||
} | ||
throw new Exception('Failed to create cache directory.'); | ||
} | ||
|
||
public function set(string $key, mixed $value, int|DateInterval|null $ttl = 3600): bool | ||
{ | ||
$filePath = $this->getFilePath($key); | ||
$data = [ | ||
'expires_at' => time() + $ttl, | ||
'value' => serialize($value), | ||
]; | ||
|
||
return (bool) file_put_contents($filePath, serialize($data)); | ||
} | ||
|
||
public function get(string $key, mixed $default = null): mixed | ||
{ | ||
$filePath = $this->getFilePath($key); | ||
|
||
if (!file_exists($filePath)) { | ||
return null; | ||
} | ||
|
||
$data = unserialize(file_get_contents($filePath)); | ||
|
||
if ($data['expires_at'] < time()) { | ||
unlink($filePath); | ||
|
||
return null; | ||
} | ||
|
||
return unserialize($data['value']); | ||
} | ||
|
||
public function delete(string $key): bool | ||
{ | ||
$filePath = $this->getFilePath($key); | ||
|
||
if (file_exists($filePath)) { | ||
return unlink($filePath); | ||
} | ||
|
||
return false; | ||
} | ||
|
||
public function clear(): bool | ||
{ | ||
$files = glob($this->cacheDir.'/*'); | ||
|
||
foreach ($files as $file) { | ||
if (is_file($file)) { | ||
unlink($file); | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
|
||
private function getFilePath(string $key): string | ||
{ | ||
return $this->cacheDir.DIRECTORY_SEPARATOR.md5($key).'.cache'; | ||
} | ||
|
||
public function getMultiple(iterable $keys, mixed $default = null): iterable | ||
{ | ||
$items = []; | ||
foreach ($keys as $key) { | ||
$items[$key] = $this->get($key); | ||
} | ||
|
||
return $items; | ||
} | ||
|
||
public function setMultiple(iterable $values, DateInterval|int|null $ttl = null): bool | ||
{ | ||
foreach ($values as $key => $value) { | ||
$this->set($key, $value, is_int($ttl) ? $ttl : 3600); | ||
} | ||
|
||
return true; | ||
} | ||
|
||
public function deleteMultiple(iterable $keys): bool | ||
{ | ||
foreach ($keys as $key) { | ||
$this->delete($key); | ||
} | ||
|
||
return true; | ||
} | ||
|
||
public function has(string $key): bool | ||
{ | ||
return $this->get($key) !== null; | ||
} | ||
} |
Oops, something went wrong.