Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using memcache in codeigniter

I need to have library that can be used as memcache with my codeigniter. what I need to do is to extract menus from DB on user's role basis and then store the menu in cache. The idea is to show the exact menu to another user if it has the same role(without calling DB). I have a separate view for menus(Header) and includes it on each of my page(view). Here a thing to note is every time a controller is executed when requesting to a new page(view).

I just used a library https://github.com/tomschlick/memcached-library which did not work for me in this scenario. As I call the new page the header view throws an error for not defined get function for memcache (as I am getting data in header through memcache).

Is there any solid library or way to accomplish the task?

Thanks

like image 798
Tausif Khan Avatar asked Dec 06 '22 18:12

Tausif Khan


1 Answers

CodeIgniter2 has caching library, which supports Memcache https://www.codeigniter.com/user_guide/libraries/caching.html#memcached-caching

Use something lilke that:

$role_id = 2;  
$menu = $foo = $this->cache->get('menu_'.$role_id);
if (!$menu){
  $menu_data = $this->my_model->loadMenu($role_id);
  $menu = $this->load->view('menu_tpl', $menu_data, TRUE);
  $this->cache->save('menu_'.$role_id, $menu);
}

Sample memcache.php from the CI forum

<?php
  if (!defined('BASEPATH')) exit('No direct script access allowed');

  $config['memcached'] = array(
          'hostname' => '127.0.0.1',
          'port'        => 11211,
          'weight'    => 1
  );
?> 
like image 143
AquilaX Avatar answered Jan 10 '23 04:01

AquilaX