static Method-PHP code refactoring puzzle, ask for answers

Source: Internet
Author: User
Keywords PHP refactoring static methods
Just finished writing a part of the background function, now plan to refactor the code, but some questions, do not know how to deal with.

Function Description:
1. There are devices and groups under the user, devices can be grouped, and devices between groups can pass data to each other. Device on-line, find Redis has not cached users, groups, devices, if the cache is just a simple update of the cached individual data, if not so first look from the database, look after the Redis cache.
2. Now is the use of Usercache, Groupcache, Devicecache three classes, to achieve the cache read and write, UserDB, groupdb, devicedb three classes to implement the database read and write. And their methods are static methods that call their methods directly in the server.

Refactoring: Now you want to encapsulate the cache layer and the DB layer, such as the Devicedatahandler package Devicecache and Devicedb, and call Devicedatahandler directly in the server for device data access processing.

例如: Server中收到一个设备上线数据,现在处理是 (1)Server先调用DeviceDB的getDeviceInfo方法从数据库里取设备信息.(2)Server再调用DevieCache的cacheDeviceInfo方法缓存数据。 现在想Server直接用DeviceDataHandler 的 deviceOnLineMsg()方法完成上面两步的处理。

Doubts: 1. There are static methods in both the cache layer and the DB layer, and if you only call the DataHandler method in the server to process the data, there are some methods in the DataHandler that simply call the cache layer and the DB layer method, which increases the workload. has no other role. So I am to keep the status quo is not refactoring, or refactoring into DataHandler, let DataHandler as the external interface of data access?
2: Is it a static method if encapsulated in DataHandler? So that is the static method in DataHandler, the Cache, DB layer is also a static method, how can I encapsulate?

Reply content:

Just finished writing a part of the background function, now plan to refactor the code, but some questions, do not know how to deal with.

Function Description:
1. There are devices and groups under the user, devices can be grouped, and devices between groups can pass data to each other. Device on-line, find Redis has not cached users, groups, devices, if the cache is just a simple update of the cached individual data, if not so first look from the database, look after the Redis cache.
2. Now is the use of Usercache, Groupcache, Devicecache three classes, to achieve the cache read and write, UserDB, groupdb, devicedb three classes to implement the database read and write. And their methods are static methods that call their methods directly in the server.

Refactoring: Now you want to encapsulate the cache layer and the DB layer, such as the Devicedatahandler package Devicecache and Devicedb, and call Devicedatahandler directly in the server for device data access processing.

例如: Server中收到一个设备上线数据,现在处理是 (1)Server先调用DeviceDB的getDeviceInfo方法从数据库里取设备信息.(2)Server再调用DevieCache的cacheDeviceInfo方法缓存数据。 现在想Server直接用DeviceDataHandler 的 deviceOnLineMsg()方法完成上面两步的处理。

Doubts: 1. There are static methods in both the cache layer and the DB layer, and if you only call the DataHandler method in the server to process the data, there are some methods in the DataHandler that simply call the cache layer and the DB layer method, which increases the workload. has no other role. So I am to keep the status quo is not refactoring, or refactoring into DataHandler, let DataHandler as the external interface of data access?
2: Is it a static method if encapsulated in DataHandler? So that is the static method in DataHandler, the Cache, DB layer is also a static method, how can I encapsulate?

Personally think
1. It is necessary to encapsulate, you said before

Device on-line, find Redis has not cached users, groups, devices, if the cache is just a simple update of the cached individual data, if not so first look from the database, look after the Redis cache.

So this logic should be handled in DataHandler, and you should just get the information from the device, and the external app should not care if you're using the cache or what cache it is, whether it's redis or something. In the future, even if the logic of your access cache changes, you can not modify the external code. such as not DB, directly on the Redis landing or any other solution

2. Whether to use static methods and your problem seems to be not very related, you first understand why you use static methods. Because you gave only local information, I can't tell if you have the need to use a static method. Furthermore, if you have to use a static method in DataHandler. DB does not need to be used because their interface does not provide access for DataHandler access only

  • 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.