PHP code example of alvin0 / redis-model

1. Go to this page and download the library: Download alvin0/redis-model 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/ */

    

alvin0 / redis-model example snippets


use Alvin0\RedisModel\Model;

class User extends Model {

    /**
     * The model's sub keys for the model.
     *
     * @var array
     */
    protected $subKeys = [
        'name',
        'role',
    ];

    /**
     * The attributes that are mass assignable.
     *
     * @var array<string>
     */
    protected $fillable = [
            'id',
            'email',
            'name',
            'role',
            'address'
    ];
}

use Alvin0\RedisModel\Model;

class User extends Model {
    /**
     * The primary key for the model.
     *
     * @var bool
     */
    protected $primaryKey = 'email';
    
    /**
     * Indicates if the IDs are auto-incrementing.
     *
     * @var bool
     */
    public $incrementing = false;

    /**
     * The model's sub keys for the model.
     *
     * @var array
     */
    protected $subKeys = [
        'name',
        'role',
    ];

    /**
     * The attributes that are mass assignable.
     *
     * @var array<string>
     */
    protected $fillable = [
            'email',
            'name',
            'role',
            'address'
    ];
}

use Alvin0\RedisModel\Model;

class User extends Model {
    // ...
}

use Alvin0\RedisModel\Model;

class User extends Model {
    /**
     * The model's table.
     *
     * @var array
     */
    protected $table = "";

    /**
     * The model's prefixTable.
     *
     * @var array
     */
    protected $prefixTable = null;
}

use Alvin0\RedisModel\Model;

class User extends Model {
    /**
     * Indicates if the model should be timestamped.
     *
     * @var bool
     */
    public $timestamps = false;
}

use Alvin0\RedisModel\Model;

class User extends Model {

    /**
     * @var string|null
     */
    protected $connectionName = null;
}

use App\RedisModels\User;

User::where('email', '[email protected]')
    ->where('role', 'admin')
    ->get();

use App\RedisModels\User;

User::where('name', "user_*")->get();
// result collection 
// [
//  ["name" => "user_1"],
//  ["name" => "user_2"],
//  ["name" => "user_3"],
//  ["name" => "user_4"],
// ]

use App\RedisModels\User;

User::all();

use App\RedisModels\User;

User::where('name', "user_*")->get();

use App\RedisModels\User;
use Alvin0\RedisModel\Collection;

User::where('user_id', 1)
    ->chunk(10, function (Collection $items) {
        foreach ($items as $item) {
            dump($item);
        }
    });

use App\RedisModels\User;
 
// Retrieve a model by its primary key...
$user = User::find('value_primary_key');
 
// Retrieve the first model matching the query constraints...
$user = User::where('email', '[email protected]')->first();


use App\RedisModels\User;

$user = new User;
$user->email = '[email protected]';
$user->name = 'Alvin0';
$user->token = '8f8e847890354d23b9a762f4d2612ce5';
$user->token = now();
$user->save()

use App\RedisModels\User;

$user = User::create([
    'email' => '[email protected]',
    'name' => 'Alvin0'
    'token' => '8f8e847890354d23b9a762f4d2612ce5',
    'expire_at' => now(),
])
$user->email //[email protected] 

use Alvin0\RedisModel\Model;

class User extends Model {
    /**
     * Indicates when generating but key exists
     *
     * @var bool
     */
    protected $preventCreateForce = true;
}

use App\RedisModels\User;

User::forceCreate([
    'email' => '[email protected]',
    'name' => 'Alvin0'
    'token' => '8f8e847890354d23b9a762f4d2612ce5',
    'expire_at' => now(),
]);

$user->email //[email protected] 


use App\RedisModels\User;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Str;

$seed = function ($limit) {
    $users = [];
    for ($i = 0; $i < $limit; $i++) {
        $users[] = [
            'email' => Str::random(10) . '@gmail.com',
            'name' => Str::random(8),
            'token' => md5(Str::random(10)),
            'expire_at' => now(),
        ];
    }

    return $users;
};

User::insert($seed(10));

use App\RedisModels\User;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Str;

$seed = function ($limit) {
    $users = [];
    for ($i = 0; $i < $limit; $i++) {
        $users[] = [
            'email' => Str::random(10) . '@gmail.com',
            'name' => Str::random(8),
            'token' => md5(Str::random(10)),
            'expire_at' => now(),
        ];
    }

    return $users;
};

User::transaction(function ($conTransaction) use ($data) {
    User::insert($seed(10), $conTransaction);
});

use App\RedisModels\User;
 
$user = User::find('[email protected]');
 
$user->name = 'Alvin1';

$user->save();

$user = User::find('[email protected]')->update(['name' => 'Alvin1']);

use App\RedisModels\User;
 
$user = User::find('[email protected]')->delete();

    User::where('email', '*@gmail.com')->destroy();

    //or remove all data model
    User::destroy();

use App\RedisModels\User;

$user = User::find('[email protected]')->setExpire(60); // The instance will have a lifespan of 60 seconds

use App\RedisModels\User;

$user = User::find('[email protected]')->getExpire(); // The remaining time to live of the instance is 39 seconds.

use App\RedisModels\User;
use Redis;

$data [
    'users:id:1:email:email@example:name:alvin:role:admin' => [
        'id' => 1,
        'email'=>'email@example'
        'name' => 'alvin',
        'role'=>'admin'
    ]
    'users:id:2:email:email@example:name:alvin:role:admin' => [
        'id' => 2,
        'email'=>'email@example'
        'name' => 'alvin',
        'role'=>'admin'
    ]
    'users:id:3:email:email@example:name:alvin:role:admin' => [
        'id' => 3,
        'email'=>'email@example'
        'name' => 'alvin',
        'role'=>'admin'
    ]
];

$user = User::transaction(function (Redis $conTransaction) use($data) {
    foreach($data as $key => $value) {
        $conTransaction->hMSet($key, $value);
    }
    // $conTransaction->discard(); 
});


php artisan vendor:publish --provider="Alvin0\RedisModel\RedisModelServiceProvider"

php artisan redis-model:model User