Skip to content

Commit

Permalink
Added option to compress the metadata in FileStorage.
Browse files Browse the repository at this point in the history
  • Loading branch information
itsgoingd committed May 1, 2019
1 parent 0a0e962 commit 8aedfa4
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 11 deletions.
29 changes: 20 additions & 9 deletions Clockwork/Storage/FileStorage.php
Expand Up @@ -14,14 +14,17 @@ class FileStorage extends Storage
// Metadata expiration time in minutes
protected $expiration;

// Compress the files using gzip
protected $compress;

// Metadata cleanup chance
protected $cleanupChance = 100;

// Index file handle
protected $indexHandle;

// Return new storage, takes path where to store files as argument, throws exception if path is not writable
public function __construct($path, $dirPermissions = 0700, $expiration = null)
public function __construct($path, $dirPermissions = 0700, $expiration = null, $compress = false)
{
if (! file_exists($path)) {
// directory doesn't exist, try to create one
Expand All @@ -43,6 +46,7 @@ public function __construct($path, $dirPermissions = 0700, $expiration = null)

$this->path = $path;
$this->expiration = $expiration === null ? 60 * 24 * 7 : $expiration;
$this->compress = $compress;
}

// Returns all requests
Expand Down Expand Up @@ -78,10 +82,12 @@ public function next($id, $count = null, Search $search = null)
// Store request, requests are stored in JSON representation in files named <request id>.json in storage path
public function store(Request $request)
{
file_put_contents(
"{$this->path}/{$request->id}.json",
@json_encode($request->toArray(), \JSON_PARTIAL_OUTPUT_ON_ERROR)
);
$path = "{$this->path}/{$request->id}.json";
$data = @json_encode($request->toArray(), \JSON_PARTIAL_OUTPUT_ON_ERROR);

$this->compress
? file_put_contents("{$path}.gz", gzcompress($data))
: file_put_contents($path, $data);

$this->updateIndex($request);
$this->cleanup();
Expand All @@ -97,15 +103,20 @@ public function cleanup($force = false)
$old = $this->searchIndexBackward(new Search([ 'time' => [ "<{$expirationTime}" ] ]));

foreach ($old as $request) {
@unlink("{$this->path}/{$request->id}.json");
$path = "{$this->path}/{$request->id}.json";
@unlink($this->compress ? "{$path}.gz" : $path);
}
}

protected function loadRequest($id)
{
if (is_readable("{$this->path}/{$id}.json")) {
return new Request(json_decode(file_get_contents("{$this->path}/{$id}.json"), true));
}
$path = "{$this->path}/{$id}.json";

if (! is_readable($this->compress ? "{$path}.gz" : $path)) return;

$data = file_get_contents($this->compress ? "{$path}.gz" : $path);

return new Request(json_decode($this->compress ? gzuncompress($data) : $data, true));
}

// Search index backward from specified ID or last record, with optional results count limit
Expand Down
5 changes: 4 additions & 1 deletion Clockwork/Support/Laravel/ClockworkSupport.php
Expand Up @@ -83,7 +83,10 @@ public function getStorage()
$storage = new SqlStorage($database, $table, null, null, $expiration);
} else {
$storage = new FileStorage(
$this->getConfig('storage_files_path', storage_path('clockwork')), 0700, $expiration
$this->getConfig('storage_files_path', storage_path('clockwork')),
0700,
$expiration,
$this->getConfig('storage_files_compress', false)
);
}

Expand Down
3 changes: 3 additions & 0 deletions Clockwork/Support/Laravel/config/clockwork.php
Expand Up @@ -146,6 +146,9 @@

'storage_files_path' => env('CLOCKWORK_STORAGE_FILES_PATH', storage_path('clockwork')),

// Compress the metadata files using gzip, trading a little bit of performance for lower disk usage
'storage_files_compress' => env('CLOCKWORK_STORAGE_FILES_COMPRESS', false),

'storage_sql_database' => env('CLOCKWORK_STORAGE_SQL_DATABASE', storage_path('clockwork.sqlite')),
'storage_sql_table' => env('CLOCKWORK_STORAGE_SQL_TABLE', 'clockwork'),

Expand Down
5 changes: 4 additions & 1 deletion Clockwork/Support/Vanilla/Clockwork.php
Expand Up @@ -146,7 +146,10 @@ protected function resolveStorage()
);
} else {
$storage = new FileStorage(
$this->config['storage_files_path'], 0700, $this->config['storage_expiration']
$this->config['storage_files_path'],
0700,
$this->config['storage_expiration'],
$this->config['storage_files_compress']
);
}

Expand Down
3 changes: 3 additions & 0 deletions Clockwork/Support/Vanilla/config.php
Expand Up @@ -61,6 +61,9 @@

'storage_files_path' => isset($_ENV['CLOCKWORK_STORAGE_FILES_PATH']) ? $_ENV['CLOCKWORK_STORAGE_FILES_PATH'] : __DIR__ . '/../../../../../clockwork',

// Compress the metadata files using gzip, trading a little bit of performance for lower disk usage
'storage_files_compress' => isset($_ENV['CLOCKWORK_STORAGE_FILES_COMPRESS']) ? $_ENV['CLOCKWORK_STORAGE_FILES_COMPRESS'] : false,

'storage_sql_database' => isset($_ENV['CLOCKWORK_STORAGE_SQL_DATABASE']) ? $_ENV['CLOCKWORK_STORAGE_SQL_DATABASE'] : 'sqlite:' . __DIR__ . '/../../../../../clockwork.sqlite',
'storage_sql_username' => isset($_ENV['CLOCKWORK_STORAGE_SQL_USERNAME']) ? $_ENV['CLOCKWORK_STORAGE_SQL_USERNAME'] : null,
'storage_sql_password' => isset($_ENV['CLOCKWORK_STORAGE_SQL_PASSWORD']) ? $_ENV['CLOCKWORK_STORAGE_SQL_PASSWORD'] : null,
Expand Down

0 comments on commit 8aedfa4

Please sign in to comment.