In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "how to use Redis in Java". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn how to use Redis in Java.
Open a remote connection
Redis does not support remote connections by default. Here, you need to open remote connections manually.
Turn off native IP binding, allowing remote connections. Find the bind:127.0.0.1 in redis.conf and comment it.
Turn on password verification. Find the requirepass in redis.conf and uncomment it and set the password.
Jedis connection Redis
Create a Maven project and import Jedis dependencies.
Redis.clients jedis jar compile
Test whether the link is successful.
Public static void main (String [] args) {/ / create a Jedis object that can be ignored if the default port is used (Redis created by Docker this time) Jedis jedis = new Jedis ("127.0.0.1", 6379); / / password authentication jedis.auth ("123456"); / / Test connection String res = jedis.ping () / / output PONG indicates that if the connection succeeds and throws an exception, it fails System.out.println (res);}
Use Redis to write some values. Those who are not familiar with the basic operation can check out the basic operation of Redis.
/ / get value System.out.println (jedis.get ("name")); / / write value String result = jedis.set ("gender", "male"); System.out.println (result)
Then go to redis to see the gender you just wrote.
The methods provided in Jedis are basically the same as the Redis commands, so I won't go into too much detail here.
Encapsulate Jedis for operation
The Jedis object is not secure, so in actual development, we usually get it through the connection pool, and then return it to the connection pool after using it.
Public interface JedisCall {void call (Jedis jedis);} public class RedisDemo {private JedisPool jedisPool; public RedisDemo () {GenericObjectPoolConfig config = new GenericObjectPoolConfig (); / / set the maximum number of free connections in the connection pool config.setMaxIdle (100); / / set the maximum number of connections config.setMaxTotal (300); / / set the maximum wait time-1 means unlimited config.setMaxWaitMillis (30000) / / enable idle time check validity config.setTestOnBorrow (true); / / Connect redis jedisPool = new JedisPool (config, "127.0.0.1", 6379, 30000);} public void execute (JedisCall jedisCall) {/ / try resource try (Jedis jedis = jedisPool.getResource ()) {jedisCall.call (jedis) } public class JedisTest {public static void main (String [] args) {RedisDemo redisDemo = new RedisDemo (); redisDemo.execute (jedis-> {/ / TODO / / do something}) }} Thank you for your reading, the above is the content of "how to use Redis in Java". After the study of this article, I believe you have a deeper understanding of how to use Redis in Java, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.