PHP code example of salibhdr / typhoon-cache

1. Go to this page and download the library: Download salibhdr/typhoon-cache 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/ */

    

salibhdr / typhoon-cache example snippets


'providers' => [

     // Other service providers...
     
     SaliBhdr\TyphoonCache\ServiceProviders\TyphoonCacheServiceProvider::class,
],

$app->register(SaliBhdr\TyphoonCache\ServiceProviders\TyphoonCacheServiceProvider::class);

$app->configure('typhoon-cache')

// typhoon-cache.php :

return [
    'cache-method' => \SaliBhdr\TyphoonCache\TyphoonCache::dispatcherEventMethod, // dispatcher,observer (change it to observer if you have another observer trait like laravel scout)
    'default-cache-ttl' => 60,// Defaults to 1 hour. in minutes || if (null)->default or (-1)->forever
    'is_cache_active' => true,
    'models' => [
        //model namespace
        App\Book::class => [
            'cache_key' => 'book', //if (null)-> sets model class name
            'cache-ttl' => 60, // Defaults to 1 hour. in minutes || if (null)->default or (-1)->forever
            'is_cache_active' => true, // true,false
            'is_based_on_user' => true, //true,false
            'cache-on' => [
                'retrieved' => false,
                'created' => false,
                'updated' => false,
                'saved' => true,
                'restored' => false,
            ],
            'delete-on' => [
                'deleted' => true,
            ],
        ],
    ],

    'routes' => [
        'api/v1/books' => [
            'is_cache_active' => true, // true,false
            'cache-ttl' => 60, // Defaults to 1 hour. in minutes || if (null)->default or (-1)->forever
            'is_based_on_user' => false, //true,false
            'prefix' => ''
        ],
    ]
];


    namespace App;

    use Illuminate\Database\Eloquent\Model
    use SaliBhdr\TyphoonCache\CacheableModel;

    class Book extends Model
    {
        use CacheableModel;
    }
 

  // typhoon-cache.php

'models' => [
        //model namespace
        App\Book::class => [...],
        App\Category::class => [...],
        App\User::class => [...],
    ],

  // typhoon-cache.php

 App\Book::class => [
            'cache_key' => 'book',
            'cache-ttl' => 60, 
            'is_cache_active' => true, 
            'is_based_on_user' => true, 
            'cache-on' => [
                'retrieved' => false,
                'created' => false,
                'updated' => false,
                'saved' => true,
                'restored' => false,
            ],
            'delete-on' => [
                'deleted' => true,
            ],
        ],
 

 // typhoon-cache.php
 
 App\Book::class => [
            'cache-on' => [
                'retrieved' => true,
            ],
            'delete-on' => [
                'deleted' => true,
            ],
        ],
 

 // typhoon-cache.php

  App\Book::class => [
             'cache-on' => [
                 'saved' => true,
             ],
             'delete-on' => [
                 'deleted' => true,
             ],
         ],
  

  // typhoon-cache.php
  
  // If model uses softdelete
  App\Book::class => [
             'cache-on' => [
                 'saved' => true,
                 'resotred' => true
             ],
             'delete-on' => [
                 'forceDeleted' => true,
                 'deleted' => true,
             ],
         ],
  

  // App\Book.php

    /**
     * gets related cache config for model
     *
     * @return array
     */
    protected function getCacheModelConfig()
    {
        return [
                'cache_key' => 'book', 
                'cache-ttl' => 60, 
                'is_cache_active' => true, 
                'is_based_on_user' => true,
                     'cache-on' => [
                         'saved' => true,
                     ],
                     'delete-on' => [
                         'deleted' => true,
                     ],
                 ];
    }
  


// App\Book.php
      /**
       * if you want to customize what you want to cache just override this method
       *
       * @return Model
       */
      public function toCacheable()
      {
      
      // method 1
        return $this;
        
      // method 2
        return $this->toArray(); 
        
      // method 3
          return [
            'title' => $this->title
            'author' => $this->author
          ]; // returns data only with these two attributes
      }


// In controller


      // if you want 1 record
      public function getBook(Request $request) 
      {
       $book = Book::findOrFail($request->get('book_id'));
       
       dd($book->isCachedData()); // returns true if its cached data
      }
      
      //if you want multipul records 
      public function getBooks(Request $request) 
      {
       $books = Book::get();
       
       foreach($books as $book)
          dd($book->isCachedData()); // returns true if its cached data
      }

 
 // In controller
 
 use SaliBhdr\TyphoonCache\Facades\TyphoonCache
 use App\Book
 
       public function getBook(Request $request) 
       {
       
        // first parametr is the model that you the data is related to.
        // second argument is the records id 
        // third argument (optional) : if is_based_on_user option in config file is set to true
        
          $book = TyphoonCache::retrieveModel(Book::class,$request->get('book_id'),auth()->id());
        
        
        dd($book->isCachedData()); // returns true if its cached data
       }
        
 

 // typhoon-cache.php :
 
 return [
     'routes' => [
         'api/v1/books' => [
             'is_cache_active' => true, 
             'cache-ttl' => 60, 
             'is_based_on_user' => true,
             'prefix' => ''
         ],
     ]
 ];
sh
php artisan vendor:publish --provider="SaliBhdr\TyphoonCache\ServiceProviders\TyphoonCacheServiceProvider"