Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

DDSameera's avatar

Create Model for Remeber::cache

Currently i m saving my cache results like this .

How could we save our cache and retrive cache values using any technique.

Eg : Create "CacheModel" class and setup methods

like that . Please provide me good example

   $permissions = Cache::remember("permissions", config('cache.expire_time.short'), function () {
            return Permissions::orderBy('name', 'ASC')->get();
        });

        $roles = Cache::remember("roles", config('cache.expire_time.short'), function () {
            return Role::all();
        });

0 likes
9 replies
fylzero's avatar

@ddsameera Sounds like what you might want to do is create a cache helper service class.

Create a folder/file in app\Services\CacheHelper.php

<?php

namespace App\Services;

use Cache;

class CacheService
{
    public static function getPermissions()
    {
        return Cache::remember('permissions', config('cache.expire_time.short'), function () {
            return Permissions::orderBy('name', 'ASC')->get();
        });
    }

    public static function getRoles()
    {
        return Cache::remember("roles", config('cache.expire_time.short'), function () {
            return Role::all();
        });
    }
}

Now, since you are using static functions here you can simply call CacheService::getPermissions() and CacheService::getRoles()after importing the service with use App\Services\CacheHelper at the top of any other class.

2 likes
DDSameera's avatar

Sorry. mistakenly it marked as a Right one .

Still i got that error

DDSameera's avatar

Please read this topic. This is not similar for that . this is different scenario.

MichalOravec's avatar

I don't care. This is my last reply to you forever. Bye.

DDSameera's avatar

One of my friend suggest me to keep these methods in model . then logic is in one file

Methods

  public static function getPermissions()
    {
        return Cache::remember('permissions', config('cache.expire_time.short'), function () {
            return Permissions::orderBy('name', 'ASC')->get();
        });
    }

    public static function getRoles()
    {
        return Cache::remember("roles", config('cache.expire_time.short'), function () {
            return Role::all();
        });
    }

Is that good approach

Please or to participate in this conversation.