Post by Joe1234 » Sat Aug 10, 2024 10:25 am

I'm using redis for an extension I'm making. I'm not sure I'm using it in the best way with what I'm doing. I'm calling the connection to redis once with every page load in the footer.

Code: Select all

			$redis = new Redis();
			$redis->connect('127.0.0.1', 6379);
Then subsequent function calls look like the following for example:

Code: Select all

			$request = $redis->get($key1);
Am I correct in that I should put the main connect on some other page that is only loaded once? And if so, which page, and how do I then make the subsequent function calls? Or is redis something that needs to connect each time the page loads like I have it now?

v3.0.4.0 php 8.1
I'm here for a reason, if your response is contact a/the developer, just don't reply.


Active Member

Posts

Joined
Sat Jan 01, 2022 5:47 am

Post by Joe1234 » Mon Aug 12, 2024 3:06 am

I wasn't thinking. I could put the connect code in the catalog/startup controller (I believe). But then how do I access/handle the $redis-> function calls on other controller pages?

v3.0.4.0 php 8.1
I'm here for a reason, if your response is contact a/the developer, just don't reply.


Active Member

Posts

Joined
Sat Jan 01, 2022 5:47 am

Post by ADD Creative » Mon Aug 12, 2024 6:36 am

You could use the $this->registry->set to store your object.

www.add-creative.co.uk


Guru Member

Posts

Joined
Sat Jan 14, 2012 1:02 am
Location - United Kingdom

Post by Joe1234 » Tue Aug 13, 2024 4:20 am

OK, thanks for the pointer, but I need a little help here. Two issues. What you pointed me to seems to solve two issues I have. One having a function work on multiple controllers and this. I couldn't quite wrap my head around how it fixed this issue, so I dove into applying it to my other issue and I'm getting a bunch of errors which I'm not understanding why I'm getting them.

First error is I'm trying to get information with "$this->config->get('whatever')" in my library file. This is working fine in the controller in the original way, so I know the parameter is correct, but when I put it in the library I get php error "PHP Fatal error: Uncaught Error: Call to a member function get() on null in.....". At the top of my library file I have

Code: Select all

		public function _construct($registry) {
			$this->config = $registry->get('config');
			$this->db = $registry->get('db');
			$this->request = $registry->get('request');
			$this->session = $registry->get('session');
		}
So it should be reading the config like any other controller, but the error persists. Any input? I've followed the instructions on the following pages with the exception that I put the call to my library in the startup file at the bottom of the index function:
https://webkul.com/blog/creating-own-li ... -opencart/
and
https://code.tutsplus.com/how-to-create ... cms-25173t
I'm also getting an error when trying to load a function from a model file even though I load the file.

Second issue is back to the original reason for this thread. I'm not seeing how to implement this in this way without consistently calling the connect for every function call to the Redis object. Could you explain it and I'll see if I can implement it?

Thanks.

v3.0.4.0 php 8.1
I'm here for a reason, if your response is contact a/the developer, just don't reply.


Active Member

Posts

Joined
Sat Jan 01, 2022 5:47 am

Post by ADD Creative » Tue Aug 13, 2024 6:37 am

Call to a member function get() on null is likely because the object you are passing to you class when you create it with New is not the registry. How are you creating an instance of your class?

I'm not too familiar with Redis, but would expect you would need to connect every request, just like you do with the database.

www.add-creative.co.uk


Guru Member

Posts

Joined
Sat Jan 14, 2012 1:02 am
Location - United Kingdom

Post by Joe1234 » Tue Aug 13, 2024 7:07 am

Turns out the issue with calling the config was I was missing an underscore in the construct function in my library file.

As far as the Redis goes, I've read, that it does not need to make a new connection with every page load by using something called "connection pooling" or "singleton pattern"....which within my readings it also states that databases can operate the same way. But I guess in the way OC is built, if the database operates that a connection needs to be made every time, I guess the same would apply to Redis. I didn't know a new connection was made every time a database query is made here, that's why I was thinking to do this with the Redis....I figured it would be good for the overhead.

Oh well, thanks for helping me resolve my other issue.

v3.0.4.0 php 8.1
I'm here for a reason, if your response is contact a/the developer, just don't reply.


Active Member

Posts

Joined
Sat Jan 01, 2022 5:47 am

Post by ADD Creative » Tue Aug 13, 2024 3:58 pm

Joe1234 wrote:
Tue Aug 13, 2024 7:07 am
I didn't know a new connection was made every time a database query is made here, that's why I was thinking to do this with the Redis....I figured it would be good for the overhead.
A new connection is not made every query, just every request (or script execution). As I understand in with database connection pooling your would still need to connect. It's just that the connection time would be reduced as a previous connection would be reused from the pool.

www.add-creative.co.uk


Guru Member

Posts

Joined
Sat Jan 14, 2012 1:02 am
Location - United Kingdom

Post by paulfeakins » Wed Aug 14, 2024 7:30 pm

Joe1234 wrote:
Tue Aug 13, 2024 7:07 am
Turns out the issue with calling the config was I was missing an underscore in the construct function in my library file.
Can we consider this issue [SOLVED]?

UK OpenCart Hosting | OpenCart Audits | OpenCart Support - please email info@antropy.co.uk


User avatar
Legendary Member

Posts

Joined
Mon Aug 22, 2011 11:01 pm
Location - London Gatwick, United Kingdom
Who is online

Users browsing this forum: Bing [Bot] and 18 guests