PHP code example of mahmoud217tr / cacheable

1. Go to this page and download the library: Download mahmoud217tr/cacheable library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

mahmoud217tr / cacheable example snippets




namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Mahmoud217TR\Cacheable\Models\Contracts\CacheableModel;
use Mahmoud217TR\Cacheable\Models\Traits\CacheableModelTrait;

class Post extends Model implements CacheableModel
{
    use CacheableModelTrait;
}



use App\Models\Post;
use Illuminate\Support\Facades\Cache;
use Mahmoud217TR\Cacheable\Facades;

# Caching the first post for 120 seconds
Post::first()->cache('first_post', 120);

# Caching published posts indefinitely
Post::whereNotNull('published_at')
    ->get()
    ->cache('published_posts');

# Retrieve cached data using the Cache facade
Cache::get('first_post');
Cache::get('published_posts');

# Or using the Cacheable facade
Cacheable::get('first_post');
Cacheable::get('published_posts');



namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Mahmoud217TR\Cacheable\Models\Contracts\CacheableModel;
use Mahmoud217TR\Cacheable\Models\Traits\CacheableModelTrait;

class Post extends Model implements CacheableModel
{
    use CacheableModelTrait;

    public static function isAutoCacheSyncEnabled(): bool
    {
        return true;
    }
}



use App\Models\Post;

# Get cached posts
$posts = Post::getCached();

# Update cached data manually
Post::syncCache();

# Flush cached data
Post::flushCache();

# Set cached data manually
Post::setCache(Post::limit(4)->get());



namespace App\Models;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Mahmoud217TR\Cacheable\Models\Contracts\CacheableModel;
use Mahmoud217TR\Cacheable\Models\Traits\CacheableModelTrait;

class Post extends Model implements CacheableModel
{
    use CacheableModelTrait;

    public function scopePublished(Builder $query)
    {
        $query->whereNotNull('published_at');
    }

    public static function isAutoCacheSyncEnabled(): bool
    {
        return true;
    }

    protected static function getDataForCaching()
    {
        return static::published()
            ->latest()
            ->get();
    }
}



namespace App\Models;

use DateInterval;
use DateTimeInterface;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Mahmoud217TR\Cacheable\Models\Contracts\CacheableModel;
use Mahmoud217TR\Cacheable\Models\Traits\CacheableModelTrait;

class Post extends Model implements CacheableModel
{
    use CacheableModelTrait;

    public function scopePublished(Builder $query)
    {
        $query->whereNotNull('published_at');
    }

    public static function isAutoCacheSyncEnabled(): bool
    {
        return true;
    }

    protected static function getDataForCaching()
    {
        return static::published()
            ->latest()
            ->get();
    }

    protected static function getCacheTTL(): null | int | DateInterval | DateTimeInterface
    {
        return 86400;
    }
}



namespace App\Models;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Mahmoud217TR\Cacheable\Models\Contracts\CacheableModel;
use Mahmoud217TR\Cacheable\Models\Traits\CacheableModelTrait;
use Mahmoud217TR\Cacheable\Models\Traits\CachedRouteBinding;

class Post extends Model implements CacheableModel
{
    use CacheableModelTrait;
    use CachedRouteBinding;

    public function scopePublished(Builder $query)
    {
        $query->whereNotNull('published_at');
    }

    public static function isAutoCacheSyncEnabled(): bool
    {
        return true;
    }

    protected static function getDataForCaching()
    {
        return static::published()
            ->latest()
            ->get();
    }
}



namespace App\Models;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Mahmoud217TR\Cacheable\Models\Contracts\CacheableModel;
use Mahmoud217TR\Cacheable\Models\Traits\CacheableModelTrait;
use Mahmoud217TR\Cacheable\Models\Traits\CachedRouteBinding;

class Post extends Model implements CacheableModel
{
    use CacheableModelTrait;
    use CachedRouteBinding;

    public function scopePublished(Builder $query)
    {
        $query->whereNotNull('published_at');
    }

    public static function isAutoCacheSyncEnabled(): bool
    {
        return true;
    }

    protected static function getDataForCaching()
    {
        return static::published()
            ->latest()
            ->get();
    }

    protected static function shouldUseDifferentDataForBinding(): bool
    {
        return true;
    }

    protected static function getBindingData()
    {
        return static::all();
    }
}



namespace App\Models;

use DateInterval;
use DateTimeInterface;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Mahmoud217TR\Cacheable\Models\Contracts\CacheableModel;
use Mahmoud217TR\Cacheable\Models\Traits\CacheableModelTrait;
use Mahmoud217TR\Cacheable\Models\Traits\CachedRouteBinding;

class Post extends Model implements CacheableModel
{
    use CacheableModelTrait;
    use CachedRouteBinding;

    public function scopePublished(Builder $query)
    {
        $query->whereNotNull('published_at');
    }

    public static function isAutoCacheSyncEnabled(): bool
    {
        return true;
    }

    protected static function getDataForCaching()
    {
        return static::published()
            ->latest()
            ->get();
    }

    protected static function shouldUseDifferentDataForBinding(): bool
    {
        return true;
    }

    protected static function getBindingData()
    {
        return static::all();
    }

    protected static function getBindingCacheTTL(): null | int| DateInterval | DateTimeInterface
    {
        return 3600;
    }
}



namespace App\Models;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Mahmoud217TR\Cacheable\Models\Contracts\CacheableModel;
use Mahmoud217TR\Cacheable\Models\Traits\CacheableModelTrait;
use Mahmoud217TR\Cacheable\Models\Traits\CachedRouteBinding;

class Post extends Model implements CacheableModel
{
    use CacheableModelTrait;
    use CachedRouteBinding;

    public function scopePublished(Builder $query)
    {
        $query->whereNotNull('published_at');
    }

    public static function isAutoCacheSyncEnabled(): bool
    {
        return true;
    }

    protected static function getDataForCaching()
    {
        return static::published()
            ->latest()
            ->get();
    }

    protected static function shouldUseDifferentDataForBinding(): bool
    {
        return true;
    }

    protected static function shouldUseDifferentDataForBinding(): bool
    {
        return true;
    }

    protected static function getBindingData()
    {
        return static::all();
    }

    protected static function shouldUseAlternativeRouteBinding(): bool
    {
        return true;
    }
}



namespace App\Models;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Mahmoud217TR\Cacheable\Models\Contracts\CacheableModel;
use Mahmoud217TR\Cacheable\Models\Traits\CacheableModelTrait;
use Mahmoud217TR\Cacheable\Models\Traits\CachedRouteBinding;

class Post extends Model implements CacheableModel
{
    use CacheableModelTrait;
    use CachedRouteBinding;

    public function scopePublished(Builder $query)
    {
        $query->whereNotNull('published_at');
    }

    public static function isAutoCacheSyncEnabled(): bool
    {
        return true;
    }

    protected static function getDataForCaching()
    {
        return static::published()
            ->latest()
            ->get();
    }

    protected static function shouldUseDifferentDataForBinding(): bool
    {
        return true;
    }

    protected static function getBindingData()
    {
        return static::all();
    }

    protected static function shouldUseAlternativeRouteBinding(): bool
    {
        return true;
    }

    protected static function alternativeRouteBinding($value, $field = null)
    {
        return parent::customRouteBinding($value, $field);
    }
}



namespace App\Classes;

use Mahmoud217TR\Cacheable\Contracts\Cacheable;
use Mahmoud217TR\Cacheable\Traits\CacheableTrait;

class MyCustomClass implements Cacheable
{
    use CacheableTrait;

    protected string $customAttribute;

    public function __construct(string $value)
    {
        $this->customAttribute = $value;
    }
}



use App\Classes\MyCustomClass;

$object = new MyCustomClass("data");

# Caching object indefinitely
$object->cache('cache_key_2');

# Caching object for 200 seconds
$object->cache('cache_key', 200);




use App\Classes\MyCustomClass;
use App\Models\Post;
use Mahmoud217TR\Cacheable\Facades\Cacheable;

# --- Drived methods from the Cache facade ---

# Returns true if cached data was found
Cacheable::has('cache_key'); 

# Returns true if cached data was not found
Cacheable::missing('cache_key');

# Returns the cached data, if not found returns a default value
Cacheable::get('cache_key', 'default value');

# Caches data for with a given key for amount of time or indefinitely
Cacheable::put('cache_key', 'data', 20); # Caches data for 20 seconds
Cacheable::set('cache_key', 'data'); # Caches data indefinitely

# Invalidate cached data with a given key
Cacheable::forget('cache_key');

# --- New features ---

# Retrieves cached data by key
# Caches the value in the given value with the key if no data found
# You can also set a time to live for cache
Cacheable::cached('cache_key', 'data', 150);

# Returns true if the given model implements the CacheableModel interface
Cacheable::isCacheableModel(Post::class);
Cacheable::isCacheableModel(Post::first());

# Returns true if the given model implements the Cacheable interface
Cacheable::isCacheableClass(MyCustomClass::class);
Cacheable::isCacheableClass(new MyCustomClass('data'));

# Returns an array of the models that implements CacheableModel interface
Cacheable::getCacheableModels();



use App\Classes\MyCustomClass;
use App\Models\Post;

# Returns true if the given model implements the CacheableModel interface
is_cacheable_model(Post::class);
is_cacheable_model(Post::first());

# Returns true if the given model implements the Cacheable interface
is_cacheable_class(MyCustomClass::class);
is_cacheable_class(new MyCustomClass('data'));

cached('cached_array_key', [1, 2, 3]) # Returns [1, 2, 3]
cached('cached_array_key') # Returns [1, 2, 3]
cached('cached_string_key', "Hello") # Returns "Hello"
cached('cached_string_key', "Ops") # Returns "Hello" because data was found

bash
php artisan vendor:publish --provider="Mahmoud217TR\Cacheable\CacheableServiceProvider"