mysql - Laravel database cache -


i'm working on ecommerce website, in each page of site, there's header contains shopping cart information, each time page header has shopping cart in loads, database gets hit. wonder if can use database cache technique reduce query times? if nothing's changed, not fetch data database unless new product(s) added shopping cart?

sure can, sounds bad idea , disencourage because cache shared whole application , shopping cart information related single user. kind of user specific information better leverage cookies or session.

anyway, here have. didn't provide code sample made generic. trick add cache key makes unique each user (such model primary key) , after adding/removing product to/from cart clear cache using previous cache key.

// code current user shopping cart (the 1 shown in header) $cart = cart::where('user_id', $user_id = auth::user()->getkey())->remember(60*2, "cartofuser$user_id")->get();  // code adds product cart cart:addproduct($product, $qty) , cache::forget('cartofuser' . auth::user()->getkey()); 

my advise not over-optimize now. i'm sure cart database query not big deal. finish application, polish it, , when working expected run profiler locate bottlenecks , optimize.


Comments

Popular posts from this blog

c++ - QTextObjectInterface with Qml TextEdit (QQuickTextEdit) -

javascript - angular ng-required radio button not toggling required off in firefox 33, OK in chrome -

xcode - Swift Playground - Files are not readable -