1
0
Эх сурвалжийг харах

Update AccountService, cache object and observe changes

Daniel Supernault 4 жил өмнө
parent
commit
b299da9311

+ 3 - 1
app/Observers/AvatarObserver.php

@@ -5,6 +5,7 @@ namespace App\Observers;
 use App\Avatar;
 use App\Avatar;
 use Illuminate\Support\Facades\Storage;
 use Illuminate\Support\Facades\Storage;
 use Illuminate\Support\Str;
 use Illuminate\Support\Str;
+use App\Services\AccountService;
 
 
 class AvatarObserver
 class AvatarObserver
 {
 {
@@ -27,7 +28,7 @@ class AvatarObserver
      */
      */
     public function updated(Avatar $avatar)
     public function updated(Avatar $avatar)
     {
     {
-        //
+        AccountService::del($avatar->profile_id);
     }
     }
 
 
     /**
     /**
@@ -64,6 +65,7 @@ class AvatarObserver
                 $disk->delete($avatar->media_path);
                 $disk->delete($avatar->media_path);
             }
             }
         }
         }
+        AccountService::del($avatar->profile_id);
     }
     }
 
 
     /**
     /**

+ 64 - 0
app/Observers/ProfileObserver.php

@@ -0,0 +1,64 @@
+<?php
+
+namespace App\Observers;
+
+use App\Profile;
+use App\Services\AccountService;
+
+class ProfileObserver
+{
+    /**
+     * Handle the Profile "created" event.
+     *
+     * @param  \App\Profile  $profile
+     * @return void
+     */
+    public function created(Profile $profile)
+    {
+        //
+    }
+
+    /**
+     * Handle the Profile "updated" event.
+     *
+     * @param  \App\Profile  $profile
+     * @return void
+     */
+    public function updated(Profile $profile)
+    {
+        AccountService::del($profile->id);
+    }
+
+    /**
+     * Handle the Profile "deleted" event.
+     *
+     * @param  \App\Profile  $profile
+     * @return void
+     */
+    public function deleted(Profile $profile)
+    {
+        AccountService::del($profile->id);
+    }
+
+    /**
+     * Handle the Profile "restored" event.
+     *
+     * @param  \App\Profile  $profile
+     * @return void
+     */
+    public function restored(Profile $profile)
+    {
+        //
+    }
+
+    /**
+     * Handle the Profile "force deleted" event.
+     *
+     * @param  \App\Profile  $profile
+     * @return void
+     */
+    public function forceDeleted(Profile $profile)
+    {
+        //
+    }
+}

+ 3 - 0
app/Providers/AppServiceProvider.php

@@ -6,6 +6,7 @@ use App\Observers\{
     AvatarObserver,
     AvatarObserver,
     NotificationObserver,
     NotificationObserver,
     ModLogObserver,
     ModLogObserver,
+    ProfileObserver,
     StatusHashtagObserver,
     StatusHashtagObserver,
     UserObserver,
     UserObserver,
     UserFilterObserver,
     UserFilterObserver,
@@ -14,6 +15,7 @@ use App\{
     Avatar,
     Avatar,
     Notification,
     Notification,
     ModLog,
     ModLog,
+    Profile,
     StatusHashtag,
     StatusHashtag,
     User,
     User,
     UserFilter
     UserFilter
@@ -41,6 +43,7 @@ class AppServiceProvider extends ServiceProvider
         Avatar::observe(AvatarObserver::class);
         Avatar::observe(AvatarObserver::class);
         Notification::observe(NotificationObserver::class);
         Notification::observe(NotificationObserver::class);
         ModLog::observe(ModLogObserver::class);
         ModLog::observe(ModLogObserver::class);
+        Profile::observe(ProfileObserver::class);
         StatusHashtag::observe(StatusHashtagObserver::class);
         StatusHashtag::observe(StatusHashtagObserver::class);
         User::observe(UserObserver::class);
         User::observe(UserObserver::class);
         UserFilter::observe(UserFilterObserver::class);
         UserFilter::observe(UserFilterObserver::class);

+ 19 - 10
app/Services/AccountService.php

@@ -14,16 +14,25 @@ class AccountService {
 
 
 	public static function get($id)
 	public static function get($id)
 	{
 	{
-		// $key = self::CACHE_KEY . ':' . $id;
-		// $ttl = now()->addSeconds(10);
-		// return Cache::remember($key, $ttl, function() use($id) {
-		// });
-		
-		$fractal = new Fractal\Manager();
-		$fractal->setSerializer(new ArraySerializer());
-		$profile = Profile::whereNull('status')->findOrFail($id);
-		$resource = new Fractal\Resource\Item($profile, new AccountTransformer());
-		return $fractal->createData($resource)->toArray();
+		if($id > PHP_INT_MAX || $id < 1) {
+			return [];
+		}
+
+		$key = self::CACHE_KEY . $id;
+		$ttl = now()->addMinutes(15);
+
+		return Cache::remember($key, $ttl, function() use($id) {
+			$fractal = new Fractal\Manager();
+			$fractal->setSerializer(new ArraySerializer());
+			$profile = Profile::whereNull('status')->findOrFail($id);
+			$resource = new Fractal\Resource\Item($profile, new AccountTransformer());
+			return $fractal->createData($resource)->toArray();
+		});	
+	}
+
+	public static function del($id)
+	{
+		return Cache::forget(self::CACHE_KEY . $id);
 	}
 	}
 
 
 }
 }