The server can be modified by multiple users in a single variable at the same time. how can this problem be solved?

Source: Internet
Author: User
How can multiple users modify a variable on the server at the same time? For example, when I set the variable $ a and multiple members call $ a, they all belong to the same object instead of being independent. But it cannot be implemented using a database. $ A = 100; when Michael Jacob accesses $ a, Change $ a to 5. then, Li Si visits $ a and sees $ a as 5. Li Si changes $ a to 50, you can modify $ a for multiple users. Similar to shared variables. How to implement it ?, Based on the mem server, multiple users can modify a variable at the same time. how can this problem be achieved?
For example, I set the variable $,
When multiple members call $ a, they all belong to the same object, rather than being independent. But it cannot be implemented using a database.
$ A = 100;

When Michael Jacob accesses $ a, Change $ a to 5;
At this time, Li Si visited $ a and saw that $ a is 5. Li Si changed $ a to 50,

You can modify $ a for multiple users. Similar to shared variables.

How to implement it?

------ Solution --------------------
Memcache-based public Variable class
PHP code
Class TShare {private static $ _ Instance; private $ memcache; private function _ construct () {$ this-> memcache = new Memcache; $ this-> memcache-> connect ('2017. 0.0.1 ', 11211) or die ("cocould not connect");} public static function getInstance () {if (empty (self ::$ _ Instance) self :: $ _ Instance = new self (); return self: $ _ Instance;} function _ set ($ name, $ val) {$ this-> memcache-> set ($ name, $ val, false, 1000);} function _ get ($ name) {return $ this-> memcache-> get ($ name );}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.