Database Component hxj. Data (22nd) (cache)

Source: Internet
Author: User

First of all, I am engaged in web development, so many of them tend to be Web-oriented. The cache used by components is system. Web. httpruntime. cache.

 

By default, components disable caching.

Therefore, you need to configure cache query.

Let's take a look at the example Configuration:

<configSections>    <section name="HxjCacheConfig" type="Hxj.Data.CacheConfiguration,Hxj.Data"/>        </configSections><HxjCacheConfig enable="true">    <entities>        <add key="NorthwindConnectionString.Products" value="60"></add>    </entities></HxjCacheConfig><connectionStrings>    <add name="NorthwindConnectionString" connectionString="Data Source=ricci\hu;Initial Catalog=Northwind;Integrated Security=True" providerName="System.Data.SqlClient"/></connectionStrings>

 

The enable of hxjcacheconfig indicates whether to enable the cache. By default, the cache is disabled unless enable = "true" is enabled explicitly ".

 

The Configuration Under the entities node is the cache configuration for object queries.

<Add key = "northwindconnectionstring. Products" value = "60"> </Add>

Indicates the Products table configuration under the connection of node name = "northwindconnectionstring" in connectionstrings.

Value = "60" indicates that the cache is 60 seconds.

 

Therefore, you must add the connectionstrings name to the object name. Otherwise, the configuration is invalid.

 

Of course, value can also be a cached dependent file.

<add key="NorthwindConnectionString.Products" value="1.txt"></add>

The 1.txt file in the root directory of the slow storage dependency program for the Products table. The component checks whether the file exists. If the file does not exist, the configuration is invalid.

 

Test the cache configuration.

List<Products> list = new List<Products>();for (int i = 0; i < 2; i++){    list.Add(DbSession.Default.From<Products>().ToFirst());}for (int i = 0; i < 3; i++){    list.Add(DbSession.Default.From<Products>().ToFirst());}

Enable = "false" is set to disable caching.

Take a look at the SQL statement executed by the component:

Text: SELECT TOP 1 * FROM [Products] WHERE ([Products].[ProductID] = @1a6b9589aa23444c9e6e13049220c19d) 
Parameters: @1a6b9589aa23444c9e6e13049220c19d[Int32] = 1
Text: SELECT TOP 1 * FROM [Products] WHERE ([Products].[ProductID] = @61ce100cf8074e76abc8d205b3ec0a2e)
Parameters: @61ce100cf8074e76abc8d205b3ec0a2e[Int32] = 1
Text: SELECT TOP 1 * FROM [Products] WHERE ([Products].[ProductID] = @c99aa7541e1543aca08c28f65c7b1d4d)
Parameters: @c99aa7541e1543aca08c28f65c7b1d4d[Int32] = 1
Text: SELECT TOP 1 * FROM [Products] WHERE ([Products].[ProductID] = @c8f9ce2df63f451aa09cbc4fa62acd9c)
Parameters: @c8f9ce2df63f451aa09cbc4fa62acd9c[Int32] = 1
Text: SELECT TOP 1 * FROM [Products] WHERE ([Products].[ProductID] = @d786265aa9bd40bfbc253814d6c16b13)
Parameters: @d786265aa9bd40bfbc253814d6c16b13[Int32] = 1

 

Five queries are performed.

Enable = "true" to enable caching.

Check the executed SQL:

Text: SELECT TOP 1 * FROM [Products] WHERE ([Products].[ProductID] = @075ce07012884a21878604c197ddb7cc)

Parameters: @075ce07012884a21878604c197ddb7cc[Int32] = 1

Only one SQL connection is executed.

The last four times are read directly from the cache.

Execute again. All the queries are returned directly from the cache without connecting to the database.

 

Test cache file dependencies:

List<Products> list = new List<Products>();for (int i = 0; i < 2; i++){    list.Add(DbSession.Default.From<Products>().Where(Products._.ProductID == 3).ToFirst());}FileHelper.AppendText(Server.MapPath("~/1.txt"), "abc");
System.Threading.Thread.Sleep(2000);

for (int i = 0; i < 3; i++){    list.Add(DbSession.Default.From<Products>().Where(Products._.ProductID == 3).ToFirst());}

View SQL:

Text: SELECT TOP 1 * FROM [Products] WHERE ([Products].[ProductID] = @aa4964dae36c479792186ea95ce10b6e) 
Parameters: @aa4964dae36c479792186ea95ce10b6e[Int32] = 3
Text: SELECT TOP 1 * FROM [Products] WHERE ([Products].[ProductID] = @e614d6bbc84c4603b159e30644665e07)
Parameters: @e614d6bbc84c4603b159e30644665e07[Int32] = 3

 

After the file is modified twice, the cache becomes invalid and the data is queried from the database again.

 

In fact, sometimes the test outputs an SQL statement, which may be executed too quickly and the cache becomes invalid too late.

So later we added system. Threading. thread. Sleep (2000 );

If you refresh the new version, you only need to output SQL statements. You only need to modify the 1.txt file and then re-query the database.

 

The query determines whether a cache configuration exists based on products in from <Products>.

 

Todatareader () queries are not cached.

Of course, other methods that use todatareader queries will not be cached, such as the exists <tentity> (whereclip where) method in the previous section.

 

The next section describes the custom cache.

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.