Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion config/redis.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
<?php

declare(strict_types=1);

return [
'host' => getenv('REDIS_HOST'),
'password' => getenv('REDIS_PASSWORD'),
'port' => env('REDIS_PORT', '6379'),
'database' => env('REDIS_DB', 0),
];
];
15 changes: 15 additions & 0 deletions src/Facades/Redis.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace XNXK\LaravelRedisHelper\Facades;

use Illuminate\Support\Facades\Facade;

class Redis extends Facade
{
public static function getFacadeAccessor(): string
{
return 'laravel-redis-helper';
}
}
58 changes: 46 additions & 12 deletions src/Redis.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
<?php

declare(strict_types=1);

namespace XNXK\LaravelRedisHelper;

use Predis\Client;
Expand All @@ -17,59 +20,91 @@ public function __construct(array $config = [])
$this->expire = -1;
$this->client = new Client([
'scheme' => 'tcp',
'host' => $config['host'] ?? '127.0.0.1',
'port' => $config['port'] ?? 6379,
'host' => $config['host'] ?? '127.0.0.1',
'port' => $config['port'] ?? 6379,
]);
}

/**
* 调用predis方法.
*
* @param array $arguments
*/
public function __call(string $name, array $arguments): void
{
$this->client->$name(...$arguments);
}

/**
* 设置操作的数据类型.
*
* @return $this
*/
public function type(string $type): static
{
$this->type = $type;

return $this;
}

/**
* 设置zset的score.
*
* @return $this
*/
public function score(int $score): static
{
$this->score = $score;

return $this;
}

/**
* 设置过期时间.
*
* @return $this
*/
public function expire(int $expire): static
{
$this->expire = $expire;

return $this;
}

public function remember(string $key, callable $callback)
/**
* Get an item from the cache, or execute the given Closure and store the result.
*/
public function remember(string $key, callable $callback): mixed
{
$map = [
'string' => [
'get' => function () use ($key) {
return $this->client->get($key);
},
'put' => function ($value) use ($key) {
'put' => function ($value) use ($key): void {
$this->client->set($key, $value);
}
},
],
'zset' => [
'get' => function () use ($key, $callback) {
$zset = $this->client->zrangebyscore($key, $this->score, $this->score);
if (is_array($zset) && count($zset) == 1) {
if (is_array($zset) && count($zset) === 1) {
return json_decode(current($zset), true);
}

// 没有匹配写入cache
$return = $callback();
$this->client->zadd($key, $this->score, json_encode($return));

return $return;
},
'put' => function ($value) use ($key) {
'put' => function ($value) use ($key): void {
$this->client->zadd($key, $this->score, json_encode($value));
}
]
},
],
];

$getType = (string)$this->client->type($key);
$getType = (string) $this->client->type($key);
if (array_key_exists($getType, $map)) {
return $map[$getType]['get']();
}
Expand All @@ -80,12 +115,11 @@ public function remember(string $key, callable $callback)
$setType = $this->type ?? '';
if (array_key_exists($setType, $map)) {
$map[$setType]['put']($getValue);
if($this->expire !== -1) {
if ($this->expire !== -1) {
$this->client->expire($key, $this->expire);
}
}

return $getValue;
}
}

5 changes: 4 additions & 1 deletion src/ServiceProvider.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
<?php

declare(strict_types=1);

namespace XNXK\LaravelRedisHelper;

class ServiceProvider extends \Illuminate\Support\ServiceProvider
Expand All @@ -25,4 +28,4 @@ public function boot(): void
__DIR__ . '/../config/redis.php.php' => config_path('redis.php'),
], 'config');
}
}
}
5 changes: 4 additions & 1 deletion src/helpers.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
<?php

declare(strict_types=1);

use XNXK\LaravelRedisHelper\Redis;

if (!function_exists('redis')) {
function redis() {
function redis()
{
return app(Redis::class);
}
}
2 changes: 2 additions & 0 deletions tests/Pest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

/*
|--------------------------------------------------------------------------
| Test Case
Expand Down
10 changes: 6 additions & 4 deletions tests/RedisTest.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
<?php
it('remember', function (){

declare(strict_types=1);
it('remember', function () {
$response = redis()->type('zset')
->score(101)
->expire(3600)
->remember('test_name', function () {
return 'test';
return 'test101';
});

expect($response)->toEqual('test');
});
expect($response)->toEqual('test101');
});
5 changes: 4 additions & 1 deletion tests/TestCase.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
<?php

declare(strict_types=1);

namespace Tests;

use Illuminate\Foundation\Bootstrap\LoadEnvironmentVariables;
Expand All @@ -23,4 +26,4 @@ protected function getEnvironmentSetUp($app)
$app->bootstrapWith([LoadEnvironmentVariables::class]);
parent::getEnvironmentSetUp($app);
}
}
}