To achieve high performance, cache is the key, and cache processing is the key
I have been paying attention to performance recently. I think back to the previous cache writing scenarios, and I am confused about synchronization.CodeAll of them are written in the business method. what's even worse is that the cache code is involved. As a result, multiple methods also involve a lot of code to process the cache, which is basically not structured.
Therefore, the eventframework is roughly written and has not been optimized. Because reflection is used, the efficiency is not high and optimization is required (just developed today ...)
Code download
This eventframework can split business methods and related events (event subscription), for example:
- Event subscription:
- After the order. Add function is called, eventframework can be called one by one based on the subscribed events (Cache processing can be placed here, And the cache processing structure is relatively controllable)
- Di injection:
- Constructor injection is not yet supported. Currently, only field and Attribute-level injection is supported.
For example
Assume that after order. Add, the following cache must be updated:
- Various order list caches in the front-end member center (multiple cache items may need to be updated)
- Cache of the Order List seen by employees who process multiple orders in the background (multiple cache items may need to be updated)
If the event framework is used, the Code is as follows:
Public Static Void Configeventprocessors ()// This is the configuration for subscribing to event and event processing processor.{ // Subscribe orderaddedevent Sysruntime. eventbus. subscribe <orderaddedevent, adminpendingorderlistcachebuilder> (); Sysruntime. eventbus. subscribe <Orderaddedevent, peruserorderlistcachebuilder> (); // Subscribe orderdeletedevent Sysruntime. eventbus. subscribe <orderdeletedevent, sampleorderdeletedeventprocessor> (); // Subscribe ordersavedevent Sysruntime. eventbus. subscribe <ordersavedevent, sampleordersavedeventprocessor> ();}
Then, paste the event to the business logic interface.
Public Interface Iorderbusiness {[raiseevent ( Typeof (Ordersavedevent)]// Multiple identical events[Raiseevent ( Typeof (Ordersavedevent)] Int Add (orderentity order); [raiseevent ( Typeof (Ordersavedevent)] Bool Save (orderentity order); [raiseevent ( Typeof (Orderdeletedevent)] Bool Delete ( Int Orderid );}
The above is a tag to trigger the event. You can also use direct triggering to trigger the event:
Public int Add (orderentity order) { /// validate console. writeline ( " In add " ); int neworderid = repository. insert (order); If (neworderid> 0 ) sysruntime. eventbus. raiseevent
(order); /// trigger event in code mode return neworderid;}
Finally, the presentation layer code:
Class Program { Static Void Main ( String [] ARGs) {startupconfig. configeventprocessors ();// Configure the association between event and event Processor Sysruntime. Container. register <iorderappfacade, orderapplication> ();// Isolate interfaces from specific implementation classesSysruntime. Container. Register <Iorderbusiness, ordercomponent>();// Same as aboveSysruntime. Container. Register <Iorderrepository, fakeorderrepository> ();// Same as aboveIorderappfacade orderapp = Sysruntime. Container. Resolve <iorderappfacade> ();// Obtain an instanceOrderapp. addorder ( " Test " );}}
Output:
Code download