Caching requests
This commit is contained in:
parent
87dbdebb1f
commit
3a828a623f
|
@ -3,6 +3,7 @@
|
||||||
namespace App\Models;
|
namespace App\Models;
|
||||||
|
|
||||||
use Illuminate\Database\Eloquent\Model;
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
use Illuminate\Support\Facades\Cache;
|
||||||
|
|
||||||
class UserData extends Model
|
class UserData extends Model
|
||||||
{
|
{
|
||||||
|
@ -11,7 +12,7 @@ class UserData extends Model
|
||||||
|
|
||||||
public static function saveData($userId, $key, $value)
|
public static function saveData($userId, $key, $value)
|
||||||
{
|
{
|
||||||
$userData = self::where('id', $userId)->first();
|
$userData = self::getCachedUserData($userId);
|
||||||
|
|
||||||
if (!$userData) {
|
if (!$userData) {
|
||||||
return "null";
|
return "null";
|
||||||
|
@ -22,11 +23,13 @@ class UserData extends Model
|
||||||
|
|
||||||
$userData->image = json_encode($data);
|
$userData->image = json_encode($data);
|
||||||
$userData->save();
|
$userData->save();
|
||||||
|
|
||||||
|
self::cacheUserData($userId, $userData);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function getData($userId, $key)
|
public static function getData($userId, $key)
|
||||||
{
|
{
|
||||||
$userData = self::where('id', $userId)->first();
|
$userData = self::getCachedUserData($userId);
|
||||||
|
|
||||||
if (!$userData || !$userData->image) {
|
if (!$userData || !$userData->image) {
|
||||||
return "null";
|
return "null";
|
||||||
|
@ -39,7 +42,7 @@ class UserData extends Model
|
||||||
|
|
||||||
public static function removeData($userId, $key)
|
public static function removeData($userId, $key)
|
||||||
{
|
{
|
||||||
$userData = self::where('id', $userId)->first();
|
$userData = self::getCachedUserData($userId);
|
||||||
|
|
||||||
if (!$userData || !$userData->image) {
|
if (!$userData || !$userData->image) {
|
||||||
return "null";
|
return "null";
|
||||||
|
@ -51,6 +54,20 @@ class UserData extends Model
|
||||||
unset($data[$key]);
|
unset($data[$key]);
|
||||||
$userData->image = json_encode($data);
|
$userData->image = json_encode($data);
|
||||||
$userData->save();
|
$userData->save();
|
||||||
|
|
||||||
|
self::cacheUserData($userId, $userData);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static function getCachedUserData($userId)
|
||||||
|
{
|
||||||
|
return Cache::remember('user_data_' . $userId, now()->addMinutes(10), function () use ($userId) {
|
||||||
|
return self::where('id', $userId)->first();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private static function cacheUserData($userId, $userData)
|
||||||
|
{
|
||||||
|
Cache::put('user_data_' . $userId, $userData, now()->addMinutes(10));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue