Setup

Let’s not rewrite to wheel here.  May I suggest you take a look at the Laravel docs, an follow the setup guide.

Usage

Assuming you have everything setup already.  We will take a look at some methods for apply cache to your application.

Model Properties (Age)

Let’s take a look at calculating a users age from their date of birth.  First, we will create an Accessor method called getAgeAttribute() to calculate their age.  Be sure to have the dateOfBirth attribute in the models $dates property.

class User {
    protected $dates = ['date_of_birth'];

    public function getAgeAttribute() {
        if ($this->dateOfBirth) {
            return $this->dateOfBirth->diff(Carbon::now())->format("Y");
        }
    }
}

Pretty straight forward. However, if we have a page that lists 50+ users, each calculating a users age.  This could have quiet the impact on the server load.  So, we now have an ideal situation to store values in cache.

public function getAgeAttribute() {
    return cache()->remember("user_".$this->id."_age", 5, function(){
        if ($this->dateOfBirth) {
            return $this->dateOfBirth->diff(Carbon::now())->format("Y");
        }
    });
}

This will calculate a users age once, but return the cached value for a period a 5 minutes.  Given the characteristics for the property, it’s unlikely that the value will change that often.  Consider this when applying the time period.  Let’s increase the time to 360, six hours.

Model Property (Display Name)

You might give users an option for a display name, or even calculate one given the information you have for that user:

Say the user has selected a format for their display name.  We can create an Accessor method like.

public function getDisplayNameAttribute() {
   switch ($this->displayNameType) {
       case 'FULLNAME':
           return implode(" ", array_filter([$this->forename, $this->surname]));
           break;
       case 'EMAIL':
           return $this->email;
           break;
       case 'USERNAME':
       default:
           return $this->username;
   }
}

Now we have the method in place, we can store this in cache.

public function getDisplayNameAttribute() {
    return cache()->forever("user_".$this->id."_display_name", function(){
        switch ($this->displayNameType) {
            // ...
        }
    });
}

Above, you will notice we’ve used the forever method off the cache facade.  As this value will only change when the users details are updated, we do not need to expire the cache.  Instead, we can utilise the Eloquent model events to clear the cache on save.

public static function boot() {
    parent::boot();

    static::saved(function ($item) {
        cache()->forget("user_".$item->id."_display_name");
    });
}

Finally, we should probably make it easier to clear the cache without the need for trigger the save events. So move the cache()->forget( .. ) to it’s own function like…

public static function boot() {
    parent::boot();

    static::saved(function ($item) {
        $item->clearCaches()
    });
}

public function clearCaches() {
    if ($this->id) {
        cache()->forget("user_".$this->id."_display_name");
        // Other calls to forget cache
        // cache()->forget("user_".$this->id."_age");
        // cache()->forget("user_".$this->id."_something_else");
    }
}

Components with Blade Templates

Blade Templates Components Laravel

Take your CSS framework and convert into blade components

Products and Web Services

Some of the development services we offer.

Laravel Packages

Good things come in small Packagist...