cache| Access | cache | data | Performance Introduction:
In the case of a small amount of data, the program how to write basically performance difference is not big, but when we face tens of thousands of data, I think performance is a must consider the problem, each write a method, each fill a piece of data to take into account performance problems, otherwise the server will incur huge execution cost, If the server is not performing well, you may die immediately. So in the large amount of data access to the page, we must consider how to improve the performance of the page, this article will provide a cache to improve access performance to solve this problem, to a large extent, improve the performance of the page load data. This article is an example of the Post list page loading data in forum forums.
Body:
Each forum post list information will correspond to a cache name, for example, we can set according to the law
#region--CacheName Setting--
Boardcachename = "Board" + boardid.tostring ();
#endregion
Here we also use datasets to populate the DataTable to create the data. However, because we have cache, so, after the first load of data, the data will be pressed into the cache, and then each fill the DataTable before the judge, if the cache is empty to load, if not empty, then do not load.
Private DataTable builddatatable ()
{
Data caching mechanism
if (Cache[boardcachename]!= null)
{
Create DataTable from Cache
DataTable dt = (DataTable) Cache[boardcachename];
return DT;
}
Else
{
Create DataTable from DataBase
DataTable dt = new DataTable ();
#region-Create DataTable-
Dt. Columns.Add ("TopicID", System.Type.GetType ("System.Int32"));
Save n The addition of a similar field
Dt. Columns.Add ("Coolstate", System.Type.GetType ("System.Int32"));
Datacolumn[] keys = new datacolumn[1];
Keys[0] = dt. Columns[0];
Dt. PrimaryKey = keys;
#endregion
#region--Add DataRow--
Bbstopiccollection BTC = new Bbstopiccollection ();
Btc. Boardid = This.boardid;
Btc. Topicstate = 1;
if (!BTC. GetInfo ())
{
return DT;
}
for (int I=0;I<BTC. count;i++)
{
DataRow dr = dt. NewRow ();
Post ID
dr["TopicID"] = btc[i].id;
Save n A field-like data assignment
Cool state
dr["coolstate"] = Btc[i]. Coolstate;
Dt. Rows.Add (DR);
}
#endregion
The above code completes the data filling process, but more importantly is the data management, for example, we change a residence some status to realize some function, for example, we add the post to "cool paste", this time will be the cache to operate, special attention, We also specifically set the TopicID column as the primary value key for the table in the code above so that we can quickly navigate to the data information to be managed, as follows.
#region--Cache Management--
if (cache["Board" + this.boardID.ToString ())!= null)
{
DataTable dt = (DataTable) cache["Board" + this.boardID.ToString ()];
DataRow dr = dt. Rows.find (TOPICID);
if (Dr!= null)
{
dr["Coolstate"] = 1;
Dr. AcceptChanges ();
Dt. AcceptChanges ();
}
}
#endregion
Note: One of the topicid is transmitted through some way the unique identification field of the information you want to manipulate, AcceptChanges method update saves all changes to the corresponding object data since the last update, and after the cache has been manipulated, remember to rebind the data. In addition, we should update the database at the same time, this article thinks that the reader has the ability to operate the database data, then do not make 贅.
So how do we delete data records? Can we go directly under the line where Dr is found, with a
Dr. Delete () to remove data from the finished? The answer is, this will be a problem, the test of the cache add and update operation will take effect immediately, but delete a record of the action does not immediately work, which will result in the operation of the data asynchronous, this is not possible, At the same time 1.1 version is slightly better than the 1.0 version, but still can not solve the problem of asynchrony, so we have to wipe out the cache, refill it, if you are willing to do so, nature is understandable, I provide another idea here for reference.
Our solution is to create a table with an additional deletion flag bit, such as Deletestate, when the original load from the database are all 1, and then after the deletion, the deletion of this information flag position 0 (do not forget to manipulate the data in the database at the same time), Then when binding to the DataView filter, DV. RowFilter = "Deletestate==1", you can simulate the deletion effect.
After the data processing, access performance will be increased by a hundredfold, data only in the cache after the failure will be reloaded, the user access to the data is the operation of the cache, and cache is a server variable, for all users to share, so, if there are 100 users access, Also is the same cache for 100 visits, and the program Access cache is very fast, if not using cache, then, we will run 100 times database operations, performance is very poor, especially when a large number of users to access the massive data, the server is miserable, So using the cache to ease the load is quite necessary and relatively good a scheme, but hard to the cache after the expiration of the first visit to the page of that user, but this sacrifice in exchange for other people's high performance is also worth it.
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.