5

I am trying to run simple code of cache using memcache in my laravel project.

I have added CACHE_DRIVER=memcached in my .env file.

I have created folder of memcache in C drive and added a file memcache.exe in that, and run in cmd by opening it as administrator.

my code in route is:

Route::get('/', function () {
//    return view('welcome');
    Cache::put('k1','created memcached memory!!',1);
    Cache::add('k2','used "add" in memcached!!',2);
    Cache::forever('k3','using forever to create cache',3);
    $k1 = Cache::get('k1','default');
    $k2 = Cache::pull('k2','default');
    $k3 = Cache::pull('k3','default');
    Cache::forget('k1');
    $check = 0;
    if(Cache::has('k1')){
        return $check = 1;
    }
});

when I run this route, i get error as

Class 'Memcached' not found

Is there any solution?

EDIT:

When i remove CACHE_DRIVER=memcached and use CACHE_DRIVER=file above code runs fine. What is correct way CACHE_DRIVER=memcached or CACHE_DRIVER=file? I had referred that from video 1

Shweta
  • 1,212
  • 4
  • 20
  • 36

2 Answers2

6

You need to install the memcached extension to your server.

If you are using linux then

sudo apt-get install php5-memcached

Here is the launchpad link and here's pecl's link

Update :

If you are using xampp in windows you should just do this

In your php.ini file just remove the semi colon before this

;extension=php_memcache.dll

to

 extension=php_memcache.dll

and then restart your server

Note :

Don't forget to restart or stop and start your server after you install this.

Sulthan Allaudeen
  • 11,330
  • 12
  • 48
  • 63
  • 1
    @jedrzej.kurylo Thanks for reminding, i will add accordingly – Sulthan Allaudeen Dec 10 '15 at 08:55
  • It can be difficult to use Memcache or Memcached in Windows; if you are only working on your localhost or your project won't have a real use, you can implement redis or files (if you use files, your project couldn't be optimized). But if your project is a real project that will have requests from thirds, you can use a Ubuntu server (considering that you only work with Laravel) or implement Redis in your Windows Server. – Gilberto Sánchez Feb 06 '17 at 23:53
  • @GilbertoSánchez Can Memcached (with a 'd') ever work on Windows? I'm having tons of trouble at http://stackoverflow.com/questions/14777857/how-to-install-and-use-memcached-in-windows-for-php#comment73465638_23320151 – Ryan Apr 04 '17 at 00:52
  • What about when getting the error? ```E: Unable to locate package php5-memcached``` or ```E: Unable to locate package php-memcached``` – JGuarate Apr 07 '21 at 19:33
0
sudo yum install memcached
sudo yum install php-memcached

Replace yum with apt-get or dnf as necessary. No other changes needed, after running these it worked.

Then run memcached --version to check if it's been installed.

Max S.
  • 1,383
  • 14
  • 25