midascodebreaker's avatar

How to Properly Cache Querries with Offset?

Hi I know Cache Works by Setting a Key But If i Use Pagination in line with Cache

let say i assign one key to the cache.

And Check the key everytime if it has a key

It Really Seems useless coz the key is being replace by another querries

every now and then during a request

for example this is the first request

select count(*) as aggregate from `products`
select * from `products` limit 3 offset 3

the second request

select count(*) as aggregate from `products`
select * from `products` limit 3 offset 6

So With this in my Hand i know the variable $products which is this in my Controller

public function show(Request $request){
        
        if (Cache::has('products')) {
            $products = Cache::pull('products');
            if ($request->ajax()) {
            return Response::json(\View::make('layouts.products')->with(compact('products'))->render());
        }
         
        }
        $products = Cache::rememberForever('products', function () {
            $products = Product::paginate(3);
            $products->setPath('/');
        return $products;
        

        });
        if ($request->ajax()) {
        $products = Cache::pull('products');
        return Response::json(\View::make('layouts.products')->with(compact('products'))->render());
        }
    }

Is being replace so Cache is not taking effect and is querring instead of loading the cache querries

How can i Cache it Correctly?

Need help thanks

0 likes
1 reply
pmall's avatar

Add the variables to the cache key ?

Please or to participate in this conversation.