Laravel Octane Cache not persistent - php

Octane Version: 1.0.8
Laravel Version: 8.50.0
PHP Version: 8.0
Server & Version: Swoole 4.6.7
Database Driver & Version: MySQL 8.0.25
Everything works as expected when using Redis for example.
cache()->store('redis')->remember("test_key", now()->addMinutes(), fn() => 'test_value');
Cache::remember() method does not store the value when using Laravel Octane Cache. (returns null)
cache()->store('octane')->remember("test_key", now()->addMinutes(), fn() => 'test_value');
I did another tests and seems that Octane store is not persistent. If I use put then get immediately will receive the value, if I use put then refresh the page the value will be null. This is only for Octane driver. Redis store works fine.
cache()->store('octane')->put("test_key", 'test_value', now()->addMinutes());
cache()->store('octane')->get("test_key"); => returns null
Redis works as expected.
cache()->store('redis')->put("test_key", 'test_value', now()->addMinutes());
cache()->store('redis')->get("test_key"); => returns test_value

I just ran into same issue. It seems the "octane" cache does not work if you try to use it via console. When I use it inside my controller (or anywhere where the process starts from any web request) it works fine.
I think, if you run any command of your application, the OS runs it separately. In this case you can't see the soole cache (because that is under a different process) and also can't use SwooleTable (Exception: Tables may only be accessed when using the Swoole server.)

Related

Cannot use apc_fetch to fetch a stored variable from the cache

Environment: PHP: version 7.3 OS: Ubuntu 18.04
References followed:
PHP - apc_store
PHP - apc_fetch
I cannot use apc_fetch from separate PHP script "file2" to access stored cache.
It does work when trigger apc_fetch from file1.
File: 1_store_variable_in_memory.php
<?php
$token = "my_token_value";
apc_store('token_1', $token);
// var_dump(apc_fetch('token_1')); // Moved to file 2
File: 2_access_memory_stored_variable.php
<?php
var_dump(apc_fetch('token_1'));
Result from file 2:
bool(false)
Expected result from file 2:
string(14) "my_token_value"
APC in PHP cli gets cleared after process is ended, moreover the memory is not shared between multiple php cli processes; because of it this is probably not the tool you want to use to solve your problem.
Try Redis or memcached if you need a cache that is persisted between processes.

Enable Memcached on App Engine runtime PHP 7.2

I'm trying since several days to enable session storage in memcache on Google App Engine, using the Symfony framework. Application starts well, Memcached instance exists, but I always end up with sessions that are not stored (CSRF on all forms, etc).
So I've created a very simple POC to check if Memcached is working, and it actually doesn't.
Here's the POC code:
app.yaml
runtime: php72
resources:
cpu: 1
memory_gb: 0.5
disk_size_gb: 10
php.ini
extension=memcached.so
index.php
<?php
$test = new \Memcached;
var_dump( $test->add('test', '42') );
echo 'value of test is: ', $test->get('test');
Result of the following code is:
bool(false)
value of test is:
Memcache looks pretty well integrated to App Engine, so I'm wondering why I can't hit it.
Debugging Memcached, the error is the following:
(0x75b27c3c040) NO SERVERS DEFINED -> libmemcached/initialize_query.cc:58
Any idea how to configure it properly?
Memcache is not supported in php72, you have to use php55 in case you need to access memcache

How do I use kafka and redis in a laravel project?

I have been having problem configuring Kafka and Redis together in Laravel.
I am able to run Redis for the use of in-memory database. So Redis works fine.
$redis = app()->make('redis');
return $redis->get('name1'); // it runs fine returning value of "name1"
I am able to configure Kafka in my windows system where I am able to produce and consume messages in terminals.
Successfully configured Rdkafka as php client library and extensions.
The package I am using in Laravel for Kafka is "superbalist/laravel-pubsub": "^3.0", "superbalist/php-pubsub-kafka": "^2.0"LINK
The below mentioned code is to subscribe and consume the message
$pubsub = app('pubsub');
$pubsub->subscribe('test1', function ($message) {
var_dump($message); // the code just stuck here
});
The browser just keeps loading and won't stop. I tried to look into the code within vendors but the response is non understandable.
My ENV as requested by the package
REDIS_HOST=localhost
REDIS_PASSWORD=null
REDIS_PORT=6379
PUBSUB_CONNECTION=redis
KAFKA_BROKERS=localhost
GOOGLE_CLOUD_PROJECT_ID=your-project-id-here
GOOGLE_CLOUD_KEY_FILE=path/to/your/gcloud-key.json
HTTP_PUBSUB_URI=null
HTTP_PUBSUB_SUBSCRIBE_CONNECTION=redis
If the Redis local server and client terminals are closed the error I get
Error while reading line from the server [tcp://localhost:9092]
Please let me know if someone have been able to configure them both in laravel.
the call to the subscribe() method is blocking, which means that the script will never finish, hence the reason why your browser never stops loading.
The PHP script where you have the call to subscribe() needs to be run from the CLI and not the browser, because that code consumes Kafka messages and needs to be always alive. If you want to publish messages to Kafka you need to use the publish() method.
From the documentation:
// consume messages
// note: this is a blocking call
$adapter->subscribe('my_channel', function ($message) {
var_dump($message);
});
// publish messages
$adapter->publish('my_channel', 'HELLO WORLD');
$adapter->publish('my_channel', ['hello' => 'world']);
$adapter->publish('my_channel', 1);
$adapter->publish('my_channel', false);

Laravel Caching with Redis is very slow

I´m making my first steps with Redis on Laravel and there is something odd I figured out.
When using Redis as a cache driver in my setup it is taking far way to much time to load a page.
How do I know? When not using the Cache facade but the Redis facade directly response times are just a fraction. I set up a laravel installation on scratch and build a migration and seeder for a simple Article model.
First I thought the items were not stored in redis as redis-cli didn´t show them when searching with KEYS *. I figured out the cache is stored in another DB with REDIS_CACHE_DB as found in config/database.php
`INFO keyspace in redis-cli lists those two DB´s named 0 and 1.
I thought the problem could be caused by my localhost setup with Mamp Pro. So I switched over to the Laravel Homestead box and uploaded my project there. Same here.
Here´s the code I´m using:
routes/web.php
use Illuminate\Support\Facades\Redis;
use Illuminate\Support\Facades\Cache;
use Illuminate\Http\Request;
use App\Article;
Route::get('/get-articles-mysql', function (Request $request) {
return response()->json(Article::take(20000)->get());
});
Route::get('/get-articles-cache', function (Request $request) {
return Cache::remember('posts', 60, function () {
return Article::take(20000)->get();
});
});
Route::get('/get-articles-redis', function (Request $request) {
if($posts = Redis::get('posts.all')) {
return response()->json(json_decode($posts));
}
$posts = Article::take(20000)->get();
Redis::set('posts.all', Article::take(20000)->get());
return response()->json($posts);
});
I´m using postman to get the response times. I made several runs as the caching routes should be slow on the first request when caching is empty. But what I get on the average is this:
http://laravel-echo.local/get-articles-mysql 583ms
http://laravel-echo.local/get-articles-redis 62ms
http://laravel-echo.local/get-articles-cache 730ms
I´m not getting this. Using the Redis facade directly is super-fast. But why is caching so slow? Yes, I double checked my .env files. There is CACHE_DRIVER=redis so I´m not using file system by accident. And I used both php artisan config:clear and php artisan cache:clear to avoid mistakes when debugging.
I see a key called "laravel_cache:posts" in redis-cli. The cached posts are there. It only takes ages to load them. I also tested the requests in Chrome. The response times are much longer but still caching takes more than mere mysql querying.
So any suggestions what could be going on here?
I know this thread is already very old, but I am still getting the same.
I am using Laragon for local development and Redis makes my API request 4x slower.
EDIT:
OMFG... I just the problem.
In my .env file I had "REDIS_HOST=localhost" and that is exactly the problem.
After I change it to "REDIS_HOST=127.0.0.1", everything is running fast.
Try it and let me know.

Memcache + PHP - Why data is not expiring?

I have a simple example where I set a value for 5 seconds. The problem is that after 5 seconds; I still get back a value when I expected 'false'.
$memcache = new Memcache;
$memcache->connect('localhost', 11211) or die ("Could not connect");
$memcache->set('foo', 'bar', 0, 5); // 5 seconds expiry
var_dump($memcache->get('foo')); // bar
sleep(10);
var_dump($memcache->get('foo')); // still shows bar
Here is the memcache server version
Server's version: 1.4.13
Late to the game, but in your code it looks like you are passing "0" for the expiration (not "5"), which translates to "never expire" Specifically:
$memcache->set('foo', 'bar', 0, 5); // 5 seconds expiry
Should be:
$memcache->set('foo', 'bar', 5); // 5 seconds expiry
Unless I'm misunderstanding the PHP documentation located here, which shows that the set command takes three parameters:
public bool Memcached::set ( string $key , mixed $value [, int $expiration ] )
Edit: Whoops, I see that you're using the Memcache extension and not Memcached, which does have four parmaters. Perhaps try using the MEMCACHE_COMPRESSED constant instead of 0 to see if it works:
$memcache->set('foo', 'bar', MEMCACHE_COMPRESSED, 5); // 5 seconds expiry
There was a bug in the memcached server including version 1.4.13 through at least 1.4.14 that, once the memcached server had been running for some time, it would sometimes get into a mode where it would fail to properly expire values.
Restarting the service fixed it for me, and I'm hopeful that the newer versions fix it more permanently.
As the code looks fine - the next chain down the line is to look at either your version of the memcache PHP extension not working, or memcached server itself.
This get a but tricky. Easiest is to rule out memcached server first. (There's a php interface you can install - but that won't help you work outwhich bit.) So...
In terminal (or command window in Windows) type
telnet localhost 11211
(Note - telnet client is not installed in Windows by default - go to "control panel", "turn windows features on or off" and add from there.)
This gives you access to memcached.
Then type
stats items
which lists the memory items in memcached.
Hopefully you've only got one slab, so note its number and type
stats cachedump [Number] 0
And this will list what's recorded in cache.
If this still shows "bar" (in encoded format) then it's memcached server that's not working - upgrade for a newer version.
If this doesn't show "bar" (or, preferably, the item just doesn't exist - you get ERROR instead) then it'll be the memcache extension to PHP that's not working. Again, check your version of that.
When done, type
quit
Alternative is to check out "memcached" (php extension) and rewrite your PHP code with those classes. It's newer. If that still fails, it's definately memcached server; if that works that it was the php memcache extension.

Categories