Open-source social media scheduler built for AI agents and the teams behind them. Create, schedule and publish posts to every network through MCP or the REST API, or write them yourself in a composer your team will love.
PHP ai-agentsblueskyfacebookinstagramlaravellinkedinmastodonmcpmcp-serveropen-sourcerest-apiself-hostedsocial-mediasocial-media-managementsocial-media-schedulerthreadstiktokvuejsxyoutube
fix: cast cached post count to int and align local cache default to redis
Paulo Castellanoe35b8df86ac0
diff --git a/app/Models/Traits/HasUsage.php b/app/Models/Traits/HasUsage.phpindex e0c3a07ee..e97f8f72b 100644--- a/app/Models/Traits/HasUsage.php+++ b/app/Models/Traits/HasUsage.php@@ -66,6 +66,15 @@ trait HasUsage}/**+ * The `(int)` cast on the cached value is load-bearing: Laravel's+ * RedisStore skips serialize()/unserialize() for is_numeric values so+ * they can be incremented atomically — the side effect is that an int+ * stored via `Cache::put` comes back as a string on read. Without the+ * cast, the strict `: int` return type throws a TypeError under the+ * Redis cache driver (production). File/array/database drivers don't+ * have this optimisation and preserve the type, which is why the bug+ * never surfaced in tests or local dev.+ ** @param array<int, string> $workspaceIds*/private function cachedPostCount(array $workspaceIds): int@@ -74,7 +83,7 @@ trait HasUsagereturn 0;}- return Cache::remember(+ return (int) Cache::remember("account:{$this->id}:posts_count",self::POST_COUNT_CACHE_TTL,fn () => Post::whereIn('workspace_id', $workspaceIds)->count(),diff --git a/config/cache.php b/config/cache.phpindex fe802996a..a5b1c67ba 100644--- a/config/cache.php+++ b/config/cache.php@@ -17,7 +17,7 @@ return [|*/- 'default' => env('CACHE_STORE', 'database'),+ 'default' => env('CACHE_STORE', 'redis'),/*|--------------------------------------------------------------------------diff --git a/tests/Feature/Models/HasUsageTraitTest.php b/tests/Feature/Models/HasUsageTraitTest.phpindex e19eea154..fdbbcaddf 100644--- a/tests/Feature/Models/HasUsageTraitTest.php+++ b/tests/Feature/Models/HasUsageTraitTest.php@@ -104,3 +104,22 @@ test('postCount returns zero without querying when account has no workspaces', f// No cache entry should be written for the empty case.expect(Cache::has("account:{$this->account->id}:posts_count"))->toBeFalse();});++test('postCount survives a string-typed cache value (Redis serializer quirk)', function () {+ // Laravel's RedisStore stores numeric values raw (not serialised) so they+ // can be INCRemented atomically. The side effect: an int written via+ // Cache::put comes back as a string on read. The test driver is `array`+ // which preserves type, so we seed the cache with a literal string here+ // to mimic what production sees and assert the return type stays int.+ Workspace::factory()->create([+ 'account_id' => $this->account->id,+ 'user_id' => $this->owner->id,+ ]);++ Cache::put("account:{$this->account->id}:posts_count", '42', 300);++ $usage = $this->account->usage();++ expect($usage['postCount'])->toBe(42);+ expect($usage['postCount'])->toBeInt();+});