-
Notifications
You must be signed in to change notification settings - Fork 1
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
8a308eb
commit 9b7b4df
Showing
1 changed file
with
79 additions
and
1 deletion.
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 |
---|---|---|
@@ -1 +1,79 @@ | ||
# redis-model | ||
# redis-model | ||
|
||
## 安装 | ||
~~~ | ||
composer require limingxinleo/redis-model | ||
~~~ | ||
|
||
## 使用方法 | ||
BaseModel.php | ||
~~~ | ||
<?php | ||
namespace App\Models\RedisModel; | ||
use limx\utils\RedisModel\Model; | ||
class BaseModel extends Model | ||
{ | ||
protected function initRedisClient($parameters, $options) | ||
{ | ||
if (!isset($parameters['host'])) { | ||
$parameters['host'] = env('REDIS_HOST', 'localhost'); | ||
} | ||
if (!isset($parameters['port'])) { | ||
$parameters['port'] = env('REDIS_PORT', 6379); | ||
} | ||
if (!isset($parameters['auth'])) { | ||
$parameters['password'] = env('REDIS_AUTH', null); | ||
} | ||
if (!isset($parameters['database'])) { | ||
$parameters['database'] = env('REDIS_INDEX', 0); | ||
} | ||
parent::initRedisClient($parameters, $options); | ||
} | ||
} | ||
~~~ | ||
|
||
User.php | ||
~~~ | ||
<?php | ||
namespace App\Models\RedisModel; | ||
class User extends BaseModel | ||
{ | ||
protected $key = 'redisdmodel:user:{id}'; | ||
protected $type = 'hash'; | ||
protected $fillable = ['id', 'username', 'name']; | ||
public function replace($primaryKey, $data) | ||
{ | ||
$info = array_intersect_key($data, array_flip((array)$this->fillable)); | ||
$data = array_merge(array_fill_keys($this->fillable, ''), $info); | ||
return $this->create($primaryKey, $data); | ||
} | ||
public function destroy($primaryKey) | ||
{ | ||
if (!is_array($primaryKey)) { | ||
$primaryKey = [$primaryKey]; | ||
} | ||
return $this->whereIn('id', $primaryKey)->delete(); | ||
} | ||
public function flushAll() | ||
{ | ||
return $this->delete(); | ||
} | ||
} | ||
~~~ |