Skip to content
This repository has been archived by the owner on Apr 4, 2022. It is now read-only.

Caching #1

Open
driesvints opened this issue Aug 21, 2021 · 3 comments
Open

Caching #1

driesvints opened this issue Aug 21, 2021 · 3 comments
Labels
enhancement New feature or request

Comments

@driesvints
Copy link
Owner

driesvints commented Aug 21, 2021

Performing GraphQL calls can be costly and take quite a bit of time. Ideally I'd like to have dedicated caching support in the library. Most calls to check for sponsorships of GitHub users or to retrieve a list of sponsors don't need to be checked every second.

The way I see it we can ship a decorator for the GitHubSponsors client. This would look like as follows:

final class CachedGitHubSponsors
{
    public function __construct(
        private GitHubSponsors $client
    ) {}
    
    public function isSponsoredBy(string $account, string $sponsor, bool $isAccountAnOrganization = false): bool
    {
        // check cached status

        return $this->client->isSponsoredBy($account, $sponsor, $isAccountAnOrganization);
    }

    // ...
}

Maybe a common interface is needed?

I think this class should require a Psr\SimpleCache\CacheInterface implementation to handle the caching. For Laravel it can be retrieved from the already set cache store. We can add options to the github-sponsors.php config file to set the specific cache store and timeouts.

@driesvints driesvints added the enhancement New feature or request label Aug 21, 2021
@Gummibeer
Copy link
Contributor

I like the two classes approach - would only add a cached() method on the Facade so that users can easily use it like.

GithubSponsors::cached()->isSponsoredBy('Gummibeer');

@mabdalmoniem
Copy link

Hello guys! If you don't mind I'd like to work on this (this is my first public contribution so I want to make sure that I understand what do you require 😅)

I'll try to rephrase the proposed solution to make sure that I'm following:

  • We will create a common interface and a decorated class
interface GitHubSponsorsInterface
{
    public function isSponsoredBy(string $account, string $sponsor, bool $isAccountAnOrganization = false): bool;
    ...
}

final class GitHubSponsors implements GitHubSponsorsInterface
{
    // ...
}

final class CachedGitHubSponsors implements GitHubSponsorsInterface
{
    // ...
}
  • We will create a CacheDriver interface and inject it into the CachedGitHubSponsors, so that the developers can swap the Cache driver at any time
final class CachedGitHubSponsors implements GitHubSponsorsInterface
{
    public function __construct(
        private GitHubSponsors $client,
        private CacheDriver $cacheDriver
    ) {}
}
  • Before we call any Client method that requires calling the Github Api (like isSponsoredBy), we will check if the Cache driver instance already has cached the result, if it's empty we will call the Api then cache it.

Please forgive me if that's not what you want and I've wasted your time 😅🙏

@driesvints
Copy link
Owner Author

Heya! Thanks for willing to help out. We'll need to rethink this quite a bit since the internals have changed in the meantime. I atm don't have time for this so we'll need to hold off on this until I can find some time to wrap my head around it.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants