In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
This article is about the methods that redis uses on projects. The editor thought it was very practical, so I shared it with you as a reference. Let's follow the editor and have a look.
Remembering that I wanted to integrate Redis into the JavaWeb project, I searched a lot of tutorials online that were not comprehensive, and now I finally figured it out, so I'd like to share it here.
Installation of 1.Redis
I will not talk about it here, there are many tutorials on the Internet, windows,Linux, my own is built on the server.
Graphical connection
Add a cache to the business logic
1.1. Interface encapsulation
The commonly used methods of operating redis extract an interface and create two implementation classes corresponding to stand-alone version and cluster version respectively.
1.1.1. Interface definition
# # jedisClient
Package cn.e3mall.common.jedis;import java.util.List;public interface JedisClient {String set (String key, String value); String get (String key); Boolean exists (String key); Long expire (String key, int seconds); Long ttl (String key); Long incr (String key); Long hset (String key, String field, String value); String hget (String key, String field); Long hdel (String key, String...) Field); Boolean hexists (String key, String field); List hvals (String key); Long del (String key);}
# JedisClientPool
Package cn.e3mall.common.jedis;import java.util.List;import redis.clients.jedis.Jedis;import redis.clients.jedis.JedisPool;public class JedisClientPool implements JedisClient {private JedisPool jedisPool;public JedisPool getJedisPool () {return jedisPool;} public void setJedisPool (JedisPool jedisPool) {this.jedisPool = jedisPool;} @ Overridepublic String set (String key, String value) {Jedis jedis = jedisPool.getResource (); String result = jedis.set (key, value); jedis.close (); return result @ Overridepublic String get (String key) {Jedis jedis = jedisPool.getResource (); String result = jedis.get (key); jedis.close (); return result;} @ Overridepublic Boolean exists (String key) {Jedis jedis = jedisPool.getResource (); Boolean result = jedis.exists (key); jedis.close (); return result;} @ Overridepublic Long expire (String key, int seconds) {Jedis jedis = jedisPool.getResource (); Long result = jedis.expire (key, seconds); jedis.close (); return result } @ Overridepublic Long ttl (String key) {Jedis jedis = jedisPool.getResource (); Long result = jedis.ttl (key); jedis.close (); return result;} @ Overridepublic Long incr (String key) {Jedis jedis = jedisPool.getResource (); Long result = jedis.incr (key); jedis.close (); return result;} @ Overridepublic Long hset (String key, String field, String value) {Jedis jedis = jedisPool.getResource (); Long result = jedis.hset (key, field, value); jedis.close (); return result } @ Overridepublic String hget (String key, String field) {Jedis jedis = jedisPool.getResource (); String result = jedis.hget (key, field); jedis.close (); return result;} @ Overridepublic Long hdel (String key, String...) Field) {Jedis jedis = jedisPool.getResource (); Long result = jedis.hdel (key, field); jedis.close (); return result;} @ Overridepublic Boolean hexists (String key, String field) {Jedis jedis = jedisPool.getResource (); Boolean result = jedis.hexists (key, field); jedis.close (); return result;} @ Overridepublic List hvals (String key) {Jedis jedis = jedisPool.getResource (); List result = jedis.hvals (key); jedis.close (); return result } @ Overridepublic Long del (String key) {Jedis jedis = jedisPool.getResource (); Long result = jedis.del (key); jedis.close (); return result;}}
# JedisClientCluster
Package cn.e3mall.common.jedis;import java.util.List;import redis.clients.jedis.JedisCluster;public class JedisClientCluster implements JedisClient {private JedisCluster jedisCluster;public JedisCluster getJedisCluster () {return jedisCluster;} public void setJedisCluster (JedisCluster jedisCluster) {this.jedisCluster = jedisCluster;} @ Overridepublic String set (String key, String value) {return jedisCluster.set (key, value);} @ Overridepublic String get (String key) {return jedisCluster.get (key);} @ Overridepublic Boolean exists (String key) {return jedisCluster.exists (key) @ Overridepublic Long expire (String key, int seconds) {return jedisCluster.expire (key, seconds);} @ Overridepublic Long ttl (String key) {return jedisCluster.ttl (key);} @ Overridepublic Long incr (String key) {return jedisCluster.incr (key);} @ Overridepublic Long hset (String key, String field, String value) {return jedisCluster.hset (key, field, value);} @ Overridepublic String hget (String key, String field) {return jedisCluster.hget (key, field);} @ Overridepublic Long hdel (String key, String...) Field) {return jedisCluster.hdel (key, field);} @ Overridepublic Boolean hexists (String key, String field) {return jedisCluster.hexists (key, field);} @ Overridepublic List hvals (String key) {return jedisCluster.hvals (key);} @ Overridepublic Long del (String key) {return jedisCluster.del (key);}}
Configuration: applicationContext-redis.xml
Encapsulated code testing
Testpublic void testJedisClient () throws Exception {/ / initialize the Spring container ApplicationContext applicationContext = new ClassPathXmlApplicationContext ("classpath:spring/applicationContext-redis.xml"); / / get the JedisClient object JedisClient jedisClient = applicationContext.getBean (JedisClient.class) from the container; jedisClient.set ("first", "100"); String result = jedisClient.get ("first"); System.out.println (result);}
Add cach
1.1.1. Functional analysis
Add a cache when querying the list of contents.
1. Query the cache before querying the database.
2. Query the result and respond directly to the result.
3. Unable to query, there is no need to query the database in the cache.
4. add the query results to the cache.
5. Return the result.
Add cache: Key:cidValue: content list to redis. You need to convert the java object to json. Use hash to classify key. HASH_KEY:HASH |-KEY:VALUE |-KEY:VALUE
Note: adding a cache does not affect normal business logic.
Code implementation (implemented at the service layer)
@ Autowiredprivate JedisClient jedisClient;@Value ("${CONTENT_LIST}") private String CONTENT_LIST;@Overridepublic E3Result addContent (TbContent content) {/ / insert content data into the content table content.setCreated (new Date ()); content.setUpdated (new Date ()); / / insert into the database contentMapper.insert (content); / / cache synchronization to delete the corresponding data in the cache. JedisClient.hdel (CONTENT_LIST, content.getCategoryId (). ToString ()); return E3Result.ok ();} @ Overridepublic List getContentListByCid (long cid) {/ / query cache try {/ / if there is a direct response result in the cache String json = jedisClient.hget (CONTENT_LIST, cid + "); if (StringUtils.isNotBlank (json)) {List list = JsonUtils.jsonToList (json,TbContent.class); System.out.println (" data found from cache "); return list } catch (Exception e) {e.printStackTrace ();} / if there is no query database TbContentExample example = new TbContentExample (); Criteria criteria = example.createCriteria (); / / set query condition criteria.andCategoryIdEqualTo (cid); / / execute query List list = contentMapper.selectByExampleWithBLOBs (example); / / add results to cache try {System.out.println ("add results to cache"); jedisClient.hset (CONTENT_LIST, cid + ", JsonUtils.objectToJson (list) } catch (Exception e) {e.printStackTrace ();} return list;}
* * pay attention to cache synchronization
One more thing, hot cache, give it an expiration time (it's okay to be out of sync)
@ Overridepublic TbItem getItemById (long itemId) {/ / get goods to add cache, without reflecting the business logic, try-catchtry {System.out.println ("cache obtaining commodity information"); String json = jedisClient.get (REDIS_ITEM_PRE+ ":" + itemId+ ": BASE"); if (StringUtils.isNotBlank (json)) {TbItem tbItem = JsonUtils.jsonToPojo (json,TbItem.class); return tbItem;}} catch (Exception e) {e.printStackTrace () } / / not in cache, query database / / query / / TbItem tbItem = itemMapper.selectByPrimaryKey (itemId) according to primary key; TbItemExample example = new TbItemExample (); Criteria criteria = example.createCriteria (); / / set query condition criteria.andIdEqualTo (itemId); / / execute query List list = itemMapper.selectByExample (example); if (list! = null & & list.size () > 0) {/ / result is added to cache try {System.out.println ("cache adds commodity information") JedisClient.set (REDIS_ITEM_PRE+ ":" + itemId+ ": BASE", JsonUtils.objectToJson (list.get (0); / / set expiration time (1 hour) jedisClient.expire (REDIS_ITEM_PRE+ ":" + itemId+ ": BASE", TIEM_CACHE_EXPIRE);} catch (Exception e) {e.printStackTrace ();} return list.get (0);} return null;}
Data in redis:
{"itemId": 1231490, "created": 1425821627000, "updated": 1425821627000, "itemDesc": "warm reminder: Xiaomi 4 Unicom version has built-in operator software, Xiaomi 4 official Unicom version does not have built-in operator software. Product display Products Exhibition
Xiaomi Mobile phone 4
Still good at reading, taking pictures, playing games and even accomplishing complex and arduous tasks on your smartphone, all of which depends on speed. Each generation of Xiaomi mobile phone uses the current advanced components. Faster processors, faster flash memory, faster cameras and image processing, support faster networks. Set the world's new technology in a 5-inch device, just to make the fun of technology available to everyone, and life is getting better.
Product information Product Information Qualcomm ®Snapdragon? 801 quad-core 2.5GHz processor Xiaomi mobile phone 4 uses Qualcomm's powerful Snapdragon 801 mobile phone processor with four Krait 400 2.5GHz processing cores. The operation speed is increased by 14%, and the performance is more powerful. It can handle multiple complex tasks at the same time. Its strength is also reflected in the fact that the speed of the image processor is nearly double that of the previous generation, which makes both photography and video recording more playable and possible. Contains a Hexagon DSP core designed to run movies, music, photography and other tasks with ultra-low power consumption. This means that while the performance is more powerful, the phone's battery life is more durable than ever.
Adreno 330GPU console-quality 3D rendering capabilities Adreno 330GPUs support advanced graphics processing API, including OpenGL ES 3.0, OpenCL, RenderscriptCompute and FlexRender. The reason why it can render complex graphics quickly is due to the unified rendering architecture and FlexRender fast rendering technology. The unified rendering architecture can dynamically adjust its resource allocation according to the type of graphics rendered, and pixel and vertex rendering can be adjusted independently. FlexRender technology can dynamically convert between graphics pixels directly or through delayed rendering mode, which helps Adreno GPU render game graphics faster and more efficiently.
High color saturation screen, 84% NTSC color gamut 17% higher than iPhone 5s Xiaomi phone 4 uses high color saturation Sharp / JDI screen, the overall color saturation increases 17%, making the color expression more rich. Whether you browse pictures, watch videos, or use the wonderful App on Xiaomi 4, you can show you a realistic color effect.
Sonny high picture quality
How to capture a fleeting moment with a mobile phone when focusing at 0.3 seconds as fast as possible? Using Sony IMX 214s second generation 1300 megapixel Exmor RS? The stack image sensor makes it easier to collect light by reducing the distance between the on-chip microlens and the photodiode. Supports photos with richer hardware and less noise. The six lens groups use closed-loop focusing technology, which can focus as fast as 0.3 seconds, which is more than twice as fast as mainstream mobile phones. It also has the largest F1.8 aperture on the phone, the darker light is better, and the background virtual effect is softer. In order to make the shooting more convenient and interesting, you can also use the magic focus function to take a picture first and then choose the focus when you look back. You don't have to practice your photography skills, but you can take interesting photos with Xiaomi mobile phone 4.
Support the latest 4G LTE network to surf the Internet, listen to music and watch video faster. The mobile 4G version supports the latest China Mobile 4G (TDD-LTE) network. The peak downlink speed can reach 132Mbps, and the upload speed can reach 31Mbps. Such a fast transmission speed can meet almost any need for wireless applications. When you travel, you can use it to watch online high-definition videos, browse the web or play the most popular online games anytime, anywhere. The extremely fast 4G network speed can bring you a sound experience. You can also buy 3G versions of Unicom and Telecom.
Use mobile phone remote control TV, air conditioner equipped with infrared transmitter, support 2853 types of equipment Xiaomi mobile phone 4 built-in infrared remote control function, support the transmission protocol with infrared receiving equipment, can be customized for this function through the millet remote control App, remote control home TV, air conditioning and other household appliances that support infrared protocol. At present, 2853 devices are supported, and more devices are constantly changing.
MIUI V5 is released in 24 languages around the world, and 65 million users have received rave reviews. MIUI is optimized for native Android deep into the bottom of the system, making it smoother and more power-efficient. The second is good-looking, thousands of original themes, tens of thousands of collocations, so that the mobile phone interface is ever-changing. More importantly, it makes your life more convenient. Automatically identify strange phone calls, identify harassing and fraudulent calls. It can also quickly find the life services you need, and even make reservations for restaurants and hospitals in the system.
"{" id ": 1231490," title ":" Xiaomi 4 White Unicom 3G mobile phone "," sellPoint ":" sold out! Stainless steel metal frame, 5-inch screen ultra-narrow edge, Snapdragon quad-core 2.5GHz processor, 3G RAM,1 thank you for reading! So much for sharing the methods used by redis on the project. I hope the above content can be of some help to you so that you can learn more knowledge. If you think the article is good, you can share it and let more people see it.
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.