PHP code example of bennett-treptow / laravel-cached-mutators

1. Go to this page and download the library: Download bennett-treptow/laravel-cached-mutators 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/ */

    

bennett-treptow / laravel-cached-mutators example snippets



use CachedMutators\HasCachedMutators;

/**
 * @property string $customer_id
 * @property \Stripe\Customer $associated_stripe_customer
 */
class MyModel extends Model {
    use HasCachedMutators;

    //declare your auto cached attribute keys
    protected $cacheAttributes = [
        'associated_stripe_customer'
    ];

    /**
    * @return \Stripe\Customer
     */
    public function getAssociatedStripeCustomer(){
        //call to an external service such as Stripe
        //this call will be proxied through the Cache
        //and will only call the external service once

        return \Stripe\Customer::retrieve($this->customer_id);
    }  
}


use CachedMutators\HasCachedMutators;

/**
 * @property string $customer_id
 * @property \Stripe\Customer $associated_stripe_customer
 * @property \Stripe\Source[] $associated_payment_methods
 */
class MyModel extends Model {
    use HasCachedMutators;

    //declare your auto cached attribute keys
    protected $cacheAttributes = [
        'associated_stripe_customer' => [
            'store' => 'redis',
            'ttl' => null
        ],
        'associated_payment_methods' => [
            'store' => 'redis',
            'ttl' => 1000
        ]
    ];

    /**
    * @return \Stripe\Customer
     */
    public function getAssociatedStripeCustomer(){
        //call to an external service such as Stripe
        //this call will be proxied through the Cache
        //and will only call the external service once

        return \Stripe\Customer::retrieve($this->customer_id);
    }  
    
    /** 
     * @return \Stripe\Source[]
     */
    public function getAssociatedPaymentMethods(){
        return \Stripe\Customer::allSources($this->customer_id, [
            'object' => 'card', 
            'limit' => 3
        ]);
    }
}


use CachedMutators\HasCachedMutators;

/**
 * @property string $customer_id
 * @property \Stripe\Customer $associated_stripe_customer
 * @property \Stripe\Source[] $associated_payment_methods
 */
class MyModel extends Model {
    use HasCachedMutators;
    
    public static function defaultCacheStore(){
        return 'redis';
    }
    public static function defaultCacheTTL(){
        return 60;
    }

    //declare your auto cached attribute keys
    protected $cacheAttributes = [
        'associated_stripe_customer', //will receive redis as its store and ttl of 60
        'associated_payment_methods' => [
            'ttl' => 600 //will override the default specified above
        ]
    ];

    /**
    * @return \Stripe\Customer
     */
    public function getAssociatedStripeCustomer(){
        //call to an external service such as Stripe
        //this call will be proxied through the Cache
        //and will only call the external service once

        return \Stripe\Customer::retrieve($this->customer_id);
    }  
    
    /** 
     * @return \Stripe\Source[]
     */
    public function getAssociatedPaymentMethods(){
        return \Stripe\Customer::allSources($this->customer_id, [
            'object' => 'card', 
            'limit' => 3
        ]);
    }
}


$myModel = new MyModel();
$stripeCustomer = $myModel->associated_stripe_customer;

//do some stuff..

$myModel->clearCachedMutators(); //will clear all declared mutators in $cacheAttributes
$myModel->clearCachedMutators('associated_stripe_customer'); //to just clear one key