19

I have been wondering always, where is all the registry data stored in Magento ?

I know that even singleton objects are stored in the registry. And registry is just a static array variable of the Mage class.

I have a couple of questions here to clear my confusion:

  1. Is registry different for different users ? I mean is it created on a per-user (per HTTP request) basis?

For example,

Mage::register('foo', 'Hello World'); //set a value for foo
Mage::registry('foo'); //will this return hello world for all HTTP users ?
  1. Is registry data stored in sessions ? If not how will Magento identify which user invoked the registry data ?

I have read Alan Storm's blog article as well as his answer on StackOverFlow regarding this. But I couldn't kill the confusion. Please correct me If I you think my basics are messed up. Thanks

3

4 Answers 4

27

The registry stores data to memory which is specific to that request (rather than user or anything else), and persists for the duration of that request only. The principle is very simple really, the Mage class is instantiated as a singleton object for every request and the instantiated Mage object remains in memory, and is accessible in all classes (and templates) until the request completes and the response is sent.

As the object is a singleton, whenever you access it you get the same object. All that is happening is that you are storing values to this object, so when one class stores a value, and another accesses it they are both working on the same object and the second class is able to retrieve the value the first class set.

4
  • 2
    So its alive only for ONE request ? Meaning if I load a page in Magento, one request is DONE and registry data is flushed ?
    – Hashid
    Commented Nov 17, 2014 at 11:08
  • 5
    Yes values you set with Mage::register() persist for that request only. If you want to store user specific data you should use the session, the registry is really just designed to make data portable between classes. Commented Nov 17, 2014 at 11:13
  • 1
    that makes sense! Same applies to singleton instance as well ?
    – Hashid
    Commented Nov 17, 2014 at 11:17
  • 1
    Yes exactly the same - if you look at the getSingleton() method of the Mage class it actually stores the data to the registry. Commented Nov 17, 2014 at 11:21
3

The registry is stored in memory and is per HTTP request, so you are not able to share data between different requests or users.

1
  • But how will Magento map the HTTP Request and registry ?
    – Hashid
    Commented Nov 17, 2014 at 11:03
2

Magento Registry stored in the application’s memory.

when ever your script is done running, whatever you had stored in the registry is gone, so there is no need to worry about clearing it out (unless the script you are running is storing large objects in the registry and is looping through a lot of data).

In such case, you have to unregister your entries when you are done with them.

the Registry is just a static property of the Mage class. see for details

but still I couldn’t really find out a good explanation

2) for each users there will be separate registry per request on server.

hope this will make little clear in your mind

3
0

In Magento 2 you can implement registry. I have given live example in my tutorial video you can see.

https://youtu.be/PWeDOGQbOfo

If you have any confusion regarding registry pattern you can reply me. I assure you i will solve your confusion.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.