tequilarapido / diskstore
Installs: 1 398
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 4
Forks: 0
Open Issues: 1
Requires
- php: >=7.1.3
- illuminate/contracts: 5.8.* || 6.*
- illuminate/support: 5.8.* || 6.*
This package is auto-updated.
Last update: 2024-11-14 07:00:37 UTC
README
This packages help with storing data to disk. This comes handy if persisting data in a database is not the best solution. (ie, Data size etc ...)
Storing entities to disks.
Install
composer require "tequilarapido/diskstore:^1.0.0"
Usage
- define a disk where entities will be stored in
config/filesystem.php
'disks' => [ // ... 'tweets' => ['driver' => 'local', 'root' => storage_path('/documents/locations')], // ... ],
- define your entity extending
StorableObject
. Every public property will be serialized and saved.
class FollowerLocation extends StorableObject
{
public $twitter_id;
public $location;
/** This identifies the entity. And must be unique */
public static function keyName()
{
return 'twitter_id';
}
/** Defines the disk that wil be used for storage **/
public static function diskName()
{
return 'locations';
}
public function setLocation($location) {
$this->location = $location;
return $this;
}
// ...
}
- Check if an entity is persisted to disk :
if(FollowerLocation::exists($twitter_id)) { //... }
- Save entity :
FollowerLocation::fromArray([ 'twitter_id' => $follower->twitter_id, 'location' => $location, ])->store();
- Work with an empty entity with a specific uid :
FollowerLocation::for($twitter_id)->setLocation($location)->store();
- Find an entity by its unique id
FollowerLocation::find($follower->twitter_id);
- Find an entity by its unique id or create/store on if none
FollowerLocation::findOrNew($twitter_id)->setLocation($location)->store();
- Customize what will be serialized by overriding the
toArray
method
class FollowerLocation extends StorableObject { // ... public function toArray() { return [ 'twitter_id' => '@requried! (uid)', // ... ]; } }
- Get stored entity full path file :
$path = FollowerLocation::path() ``