In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article focuses on "weed3-2.3.3. What is the cache control of the query?" interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn "weed3-2.3.3. What is the cache control of the query?"
Weed3 a mini ORM framework (only 0.1Mb)
Source code: https://github.com/noear/weed3 source code: https://gitee.com/noear/weed3
Cache control is the focus of query.
The framework provides control services. It is important to understand that it is not the caching service itself.
Cache control requires two important interface definitions:
1. Cache service adaptation interface ICacheService (its enhanced version of ICacheServiceEx is commonly used)
/ / it can be used to wrap various cache services public interface ICacheService {void store (String key, Object obj, int seconds); Object get (String key); void remove (String key); int getDefalutSeconds (); String getCacheKeyHead ();} / * * weed3 has three built-in implementation classes: * EmptyCache, empty cache * LocalCache, local cache * SecondCache, second-level cache container (can put two ICacheService together into a second-level cache service A little more nesting is a three-level cache service) * /
two。 Interface for manipulation on caching service: ICacheController
Public interface ICacheController {/ / which cache service T caching (ICacheService service) to use; / / whether to use cache T usingCache (boolean isCache); / / use cache and set time T usingCache (int seconds); / / tag T cacheTag (String tag) to cache;} with the above foundation, start using cache control now.
1. Let's get a service example first.
ICacheService cache = new LocalCache ()
two。 Use it.
With caching, the time is the default (a stable cache key is automatically generated)
Db.table ("test") .select ("*") .caching (cache) .getMapList ()
Use caching and cache for 30s
Db.table ("test") .caching (cache) .usingCache (30) / can also be placed between table () and select () .select ("*"). GetMapList ()
Add a tag to the cache (tag is equivalent to a virtual folder for caching key)
Db.table ("test") .caching (cache) .usingCache (30). CacheTag ('test_all') / / this is tag, not key .limit (10pm 20) .select ("*"). GetMapList ()
, 3. Fine control
Control the cache time according to the query results
Db.table ("test") .where ("id=?", 10) .caching (cache) .select ("*") .getItem (UserModel.class, (cu,m)-> {if (m.hot > 2) {uc.usingCache (60 * 5); / / popular user cache 5 minutes} else {uc.usingCache (30);}})
4. Cache cleanup
Take a paged query as an example
Db.table ("test") .limit (20Magne10) .caching (cache) .cacheTag ("test_all") .select ("*"). GetMapList (); db.table ("test"). Limit (30pm 10) .caching (cache) .cacheTag ("test_all") .select ("*") .getMapList (); / / No matter how many pages you have, a tag clears it cache.clear ("test_all")
5. Cache update
This is rarely used (cache that requires individual updates, redis is recommended)
Db.table ("test") .where ("id=?", 10) .caching (cache) .cacheTag ("test_" + 10) .select ("*") .getItem (UserModel.class); cache.update ("test_" + 10, (UserModel m)-> {m.sex = 10; return m;})
The cache control of the framework is also extremely free. It should be, isn't it? Ha ha.
Availability of caching services
1. Built-in caching service
Org.noear.weed.cache.EmptyCache / / empty cache
Org.noear.weed.cache.LocalCache / / lightweight local cache (based on Map implementation)
Org.noear.weed.cache.SecondCache / / second-level cache (assemble two ICacheServiceEx implementations)
two。 Extended caching service
Org.noear.weed.cache.ehcache.EhCache / / based on ehcache encapsulation
Org.noear weed3.cache.ehcache 3.2.1.1
Org.noear.weed.cache.j2cache.J2Cache / / based on J2Cache package developed by Chinese people
Org.noear weed3.cache.j2cache 3.2.1.1
Org.noear.weed.cache.memcached.MemCache / / based on memcached encapsulation
Org.noear weed3.cache.memcached 3.2.1.1
Org.noear.weed.cache.redis.RedisCache / / based on redis encapsulation
Org.noear weed3.cache.redis 3.2.1.1
You can also package your own ICacheServiceEx.
Do you want to package it yourself?
At this point, I believe you have a deeper understanding of "weed3-2.3.3. What is the cache control of the query?" you might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.