Database component Hxj.data (23) (custom cache)

Source: Internet
Author: User

The configured cache does not meet our requirements, such as sometimes the need to flush the cache, or a query cache time or cache dependency.

Flush Cache

For example:

Dbsession.default.from<products> (). Where (products._. ProductID = 1). Refresh (). Tofirst ();

The refresh () method sets the flush cache, which is not read from the cache, reads the latest data directly from the database, and caches it again.

Sets the cache validity period for a query.

For example:

Dbsession.default.from<products> (). Where (products._. ProductID = 1). Setcachetimeout (180). Tofirst ();

Set the cache time for this query to 180 seconds.

This setting overrides the default cache configuration, and the cache configuration does not have entity settings and is cached.

When the next execution:

Dbsession.default.from<products> (). Where (products._. ProductID = 1). Tofirst ();

Cache is valid in 180 seconds.

Set cache dependencies.

For example:

System.Web.Caching.CacheDependency CACHEDEP = new System.Web.Caching.CacheDependency (Server.MapPath ("~/2.txt"));

Dbsession.default.from<products> (). Where (products._. ProductID = 1). Setcachedependency (CACHEDEP). Tofirst ();

The cache dependency for setting this query cache depends on a 2.txt file.

This setting overrides the default cache configuration, and the cache configuration does not have entity settings and is cached.

When executed again:

Dbsession.default.from<products> (). Where (products._. ProductID = 1). Tofirst ();

If the 2.txt file does not change, the cache is not invalidated.

Of course, these methods can also be used at the same time.

The cache is based on the generated SQL, so the writing ensures consistency, especially the order of the conditions, in order to effectively use the cache, otherwise it will waste memory.

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.