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.