Static method-php code reconstruction questions, please answer-php Tutorial

Source: Internet
Author: User
I have just finished writing a part of the background function. now I plan to refactor the code, but I have some questions and don't know what to do. Function description: 1. the user has devices and groups. devices can be grouped, and devices in the group can transparently transmit data to each other. When the device is online, check if redis has completed some of the functions in the background. now I want to refactor the code, but I have some questions and don't know what to do.

Function description:
1. there are devices and groups under the user. devices can be grouped, and devices in the group can transparently transmit data to each other. When the device is online, check whether redis has cached users, groups, and devices. if the cached data only updates the cached data, if not, search for the cached data from the database first and then redis caches the data.
2. Currently, three classes are used: UserCache, GroupCache, and DeviceCache to read and write the cache, and UserDB, GroupDB, and DeviceDB to read and write the database. And their methods are static methods, which are called directly on the Server.

Reconstruction: now we want to encapsulate the Cache and DB layers. for example, DeviceDataHandler encapsulates DeviceCache and DeviceDB, and directly calls DeviceDataHandler on the Server to process device data access.

For example, if the Server receives online data from a device, the current processing is (1) the Server first calls the getDeviceInfo method of DeviceDB to retrieve the device information from the database. (2) The Server then calls the cacheDeviceInfo method of DevieCache to cache data. Now we want the Server to directly use the deviceOnLineMsg () method of DeviceDataHandler to complete the above two steps.

Doubt: 1. there are some static methods in the Cache layer and the DB layer. if the Server only calls the DataHandler method to process data, in this case, some methods in DataHandler only call the Cache layer and DB layer methods, which increases the workload and has no other effect. So do I keep the status quo from being restructured, or do I need to refactor it to DataHandler to make DataHandler an external interface for data access?
2: Do I still use static methods if it is encapsulated into DataHandler? The static method is used in DataHandler, and the Cache and DB layers are also static methods. how can I encapsulate them?

Reply content:

I have just finished writing a part of the background function. now I plan to refactor the code, but I have some questions and don't know what to do.

Function description:
1. there are devices and groups under the user. devices can be grouped, and devices in the group can transparently transmit data to each other. When the device is online, check whether redis has cached users, groups, and devices. if the cached data only updates the cached data, if not, search for the cached data from the database first and then redis caches the data.
2. Currently, three classes are used: UserCache, GroupCache, and DeviceCache to read and write the cache, and UserDB, GroupDB, and DeviceDB to read and write the database. And their methods are static methods, which are called directly on the Server.

Reconstruction: now we want to encapsulate the Cache and DB layers. for example, DeviceDataHandler encapsulates DeviceCache and DeviceDB, and directly calls DeviceDataHandler on the Server to process device data access.

For example, if the Server receives online data from a device, the current processing is (1) the Server first calls the getDeviceInfo method of DeviceDB to retrieve the device information from the database. (2) The Server then calls the cacheDeviceInfo method of DevieCache to cache data. Now we want the Server to directly use the deviceOnLineMsg () method of DeviceDataHandler to complete the above two steps.

Doubt: 1. there are some static methods in the Cache layer and the DB layer. if the Server only calls the DataHandler method to process data, in this case, some methods in DataHandler only call the Cache layer and DB layer methods, which increases the workload and has no other effect. So do I keep the status quo from being restructured, or do I need to refactor it to DataHandler to make DataHandler an external interface for data access?
2: Do I still use static methods if it is encapsulated into DataHandler? The static method is used in DataHandler, and the Cache and DB layers are also static methods. how can I encapsulate them?

I personally think
1. encapsulation is necessary, as you mentioned earlier

When the device is online, check whether redis has cached users, groups, and devices. if the cached data only updates the cached data, if not, search for the cached data from the database first and then redis caches the data.

This logic should be processed in DataHandler. what you expose to the outside should only obtain the device information. external applications should not care whether you use the cache or what cache, is it redis or something else. in this way, you do not need to modify external code even if the logic of your access to the cache changes. for example, you do not need to modify the external code, such as the database, directly implement it in redis or other solutions.

2. whether or not to use static methods does not seem to have much to do with your problem. you should first figure out why you use static methods. because you only gave partial information, I cannot determine whether you need to use static methods. in addition, you can use static methods in DataHandler if necessary. the database does not need to be used in the later Cache, because their interfaces do not provide external access only for DataHandler access.

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.