Class 'Memcached' not found in laravel
Asked Answered
D

4

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

Doityourself answered 10/12, 2015 at 7:58 Comment(3)
You need to install the PHP extension for memcached, otherwise memcached-related classes won't be availableDara
Can you provide me correct link for php extensions?Doityourself
Windows & Ubuntu installation notes https://mcmap.net/q/536130/-how-to-install-and-use-memcached-in-windows-for-phpSyneresis
M
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.

Markel answered 10/12, 2015 at 8:46 Comment(4)
@Dara Thanks for reminding, i will add accordinglyMarkel
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.Hibernal
@GilbertoSánchez Can Memcached (with a 'd') ever work on Windows? I'm having tons of trouble at #14778357Hubie
What about when getting the error? E: Unable to locate package php5-memcached or E: Unable to locate package php-memcachedEsposito
O
1

For Windows:

  1. Download the archive according to the PHP version. This does not work with version higher than php 8.1.0. https://pecl.php.net/package/memcached/3.2.0/windows
  2. Move this files libmemcached.dll and libhashkit.dll to the C:\Windows .
  3. Move php_memcached.dll into the C:/php/ext.
  4. Edite php.ini file. Add extension=memcached to the php.ini.
Overscore answered 29/4 at 14:45 Comment(0)
K
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.

Kinkajou answered 26/11, 2022 at 0:56 Comment(1)
You have to run sudo systemctl enable memcached and sudo systemctl start memcached after installing.Kinkajou
F
0

I was able to fix this error in my Laravel application by updating the config/cache.php file:

<?php

use Illuminate\Support\Str;

// Add this block
if (!class_exists('Memcached')) {
    include_once ("../memcached-api.php");
}

return [
...

The file is from https://github.com/virlatinus/php-memcached/blob/master/memcached-api.php

I copied the file to the root of my app and now I'm not getting the error anymore.

Featureless answered 7/8 at 6:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.