In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)05/31 Report--
< 0) { throw new RuntimeException("递增因子必须大于0"); } return redisTemplate.opsForValue().increment(key, delta); } /** * 递减 * @param key 键 * @param delta 要减少几(小于0) * @return */ public long decr(String key, long delta) { if (delta < 0) { throw new RuntimeException("递减因子必须大于0"); } return redisTemplate.opsForValue().increment(key, -delta); } // ================================Map================================= /** * HashGet * @param key 键 不能为null * @param item 项 不能为null * @return 值 */ public Object hget(String key, String item) { return redisTemplate.opsForHash().get(key, item); } /** * 获取hashKey对应的所有键值 * @param key 键 * @return 对应的多个键值 */ public Map hmget(String key) { return redisTemplate.opsForHash().entries(key); } /** * HashSet * @param key 键 * @param map 对应多个键值 * @return true 成功 false 失败 */ public boolean hmset(String key, Map map) { try { redisTemplate.opsForHash().putAll(key, map); return true; } catch (Exception e) { e.printStackTrace(); return false; } } /** * HashSet 并设置时间 * @param key 键 * @param map 对应多个键值 * @param time 时间(秒) * @return true成功 false失败 */ public boolean hmset(String key, Map map, long time) { try { redisTemplate.opsForHash().putAll(key, map); if (time >0) {expire (key, time);} return true;} catch (Exception e) {e.printStackTrace (); return false }} / * put data into a hash table and create * @ param key key * @ param item entry * @ param value value * @ return true success false failure * / public boolean hset (String key, String item, Object value) {try {redisTemplate.opsForHash (). Put (key, item, value) Return true;} catch (Exception e) {e.printStackTrace (); return false }} / * put data into a hash table. If it does not exist, create * @ param key key * @ param item entry * @ param value value * @ param time time (seconds) Note: if the existing hash table has time, it will replace the original time * @ return true success false failure * / public boolean hset (String key) String item, Object value, long time) {try {redisTemplate.opsForHash () .put (key, item, value) If (time > 0) {expire (key, time);} return true;} catch (Exception e) {e.printStackTrace (); return false }} / * Delete the value in the hash table * @ param key key cannot be null * @ param item entry can make multiple can not be null * / public void hdel (String key, Object... Item) {redisTemplate.opsForHash () .delete (key, item);} / * * Delete the value in the hash table * @ param key key cannot be null * @ param items entry can make multiple can not be null * / public void hdel (String key, Collection items) {redisTemplate.opsForHash (). Delete (key, items.toArray ()) } / * determine whether there is a value of this item in the hash table * @ param key key cannot be null * @ param item key cannot be null * @ return true false does not exist * / public boolean hHasKey (String key, String item) {return redisTemplate.opsForHash () .hasKey (key, item) } / * hash increment if it does not exist, it will create one and return the newly added value to * @ param key key * @ param item entry * @ param delta several times (greater than 0) * @ return * / public double hincr (String key, String item, double delta) {if (delta)
< 0) { throw new RuntimeException("递增因子必须大于0"); } return redisTemplate.opsForHash().increment(key, item, delta); } /** * hash递减 * @param key 键 * @param item 项 * @param delta 要减少记(小于0) * @return */ public double hdecr(String key, String item, double delta) { if (delta < 0) { throw new RuntimeException("递减因子必须大于0"); } return redisTemplate.opsForHash().increment(key, item, -delta); } // ============================set============================= /** * 根据key获取Set中的所有值 * @param key 键 * @return */ public Set sGet(String key) { try { return redisTemplate.opsForSet().members(key); } catch (Exception e) { e.printStackTrace(); return null; } } /** * 根据value从一个set中查询,是否存在 * @param key 键 * @param value 值 * @return true 存在 false不存在 */ public boolean sHasKey(String key, Object value) { try { return redisTemplate.opsForSet().isMember(key, value); } catch (Exception e) { e.printStackTrace(); return false; } } /** * 将数据放入set缓存 * @param key 键 * @param values 值 可以是多个 * @return 成功个数 */ public long sSet(String key, Object... values) { try { return redisTemplate.opsForSet().add(key, values); } catch (Exception e) { e.printStackTrace(); return 0; } } /** * 将数据放入set缓存 * @param key 键 * @param values 值 可以是多个 * @return 成功个数 */ public long sSet(String key, Collection values) { try { return redisTemplate.opsForSet().add(key, values.toArray()); } catch (Exception e) { e.printStackTrace(); return 0; } } /** * 将set数据放入缓存 * @param key 键 * @param time 时间(秒) * @param values 值 可以是多个 * @return 成功个数 */ public long sSetAndTime(String key, long time, Object... values) { try { Long count = redisTemplate.opsForSet().add(key, values); if (time >0) expire (key, time); return count;} catch (Exception e) {e.printStackTrace (); return 0 }} / * get the length of set cache * @ param key key * @ return * / public long sGetSetSize (String key) {try {return redisTemplate.opsForSet () .size (key);} catch (Exception e) {e.printStackTrace (); return 0 }} / * * remove the * @ param key key * @ param values value of value can be the number of multiple * @ return removals * / public long setRemove (String key, Object... Values) {try {Long count = redisTemplate.opsForSet (). Remove (key, values); return count;} catch (Exception e) {e.printStackTrace (); return 0 }} / / = = list== / * get the contents of the list cache * @ param key key * @ param start start * @ param end end 0 to-1 represents all values * @ return * / public List lGet (String key, long start, long end) {try {return redisTemplate.opsForList (). Range (key, start, end) } catch (Exception e) {e.printStackTrace (); return null;}} / * * get the length of the list cache * @ param key key * @ return * / public long lGetListSize (String key) {try {return redisTemplate.opsForList () .size (key) } catch (Exception e) {e.printStackTrace (); return 0;}} / * * get the value in list through the index * @ param key key * @ param index > = 0, header 0, second element of 1, and so on Index 0) expire (key, time); return true;} catch (Exception e) {e.printStackTrace (); return false }} / * put list in cache * @ param key key * @ param value value * @ return * / public boolean lSet (String key, List value) {try {redisTemplate.opsForList () .rightPushAll (key, value); return true;} catch (Exception e) {e.printStackTrace () Return false;}} / * put list into cache * * @ param key key * @ param value value * @ param time time (seconds) * @ return * / public boolean lSet (String key, List value, long time) {try {redisTemplate.opsForList () .rightPushAll (key, value) If (time > 0) expire (key, time); return true;} catch (Exception e) {e.printStackTrace (); return false }} / * modify a piece of data in list according to the index * @ param key key * @ param index index * @ param value value * @ return * / public boolean lUpdateIndex (String key, long index, Object value) {try {redisTemplate.opsForList (). Set (key, index, value); return true } catch (Exception e) {e.printStackTrace (); return false }} / * remove N values are value * @ param key key * @ param count remove number of * @ param value values * @ return removed * / public long lRemove (String key, long count, Object value) {try {Long remove = redisTemplate.opsForList (). Remove (key, count, value); return remove } catch (Exception e) {e.printStackTrace (); return 0;} Application@SpringBootApplication@MapperScan ("com.wlient.springboot_mq_redis.dao") @ EnableCachingpublic class SpringbootMqRedisApplication {public static void main (String [] args) {SpringApplication.run (SpringbootMqRedisApplication.class, args);}}
The main thing is to add @ EnableCaching annotation to enable caching
There are two ways to cache:
Method 1: directly use the RedisService encapsulated to RedisTemplate for caching operation
Controller layer
@ Autowired RedisService redisService; @ GetMapping ("selectOne2") public ServiceResult selectOne2 (Integer id) {User user = (User) redisService.get ("selectOne2::" + id); if (user = = null) {user = userService.queryById2 (id); redisService.set ("selectOne2::" + id,user);} System.out.println (user); return ServiceResult.ok (user);}
Method 2: use annotations
Acting on the Service layer
@ Service ("userService") @ CacheConfig (cacheNames = "departmentsService") public class UserServiceImpl implements UserService {@ Resource private UserDao userDao; / * query single data through ID * * @ param id primary key * @ return instance object * / @ Override @ Cacheable (key = "# root.methodName+':'+#id") public User queryById (Integer id) {User user = this.userDao.selectById (id) Return user;}}
Annotations are convenient to use, but not flexible enough.
Using wrapper classes increases the amount of code, but meets most of the requirements
Detailed explanation of configuration file
RedisConfig:redis cache, setting the serialization mode of key,value, etc.
SpringCacheConfig:spring cache configuration
These two files are indispensable. If the SpringCacheConfig configuration file is missing, the data stored in redis will be garbled.
CacheConfig: optional, only the configuration adopted according to the requirements (copy from the project)
After reading this, the article "how to integrate SpringBoot with Redis to enable caching mechanism" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it yourself. If you want to learn more about related articles, please follow the industry information channel.
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.