In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly shows you "how to operate Redis in Java", the content is easy to understand, clear, hope to help you solve doubts, the following let the editor lead you to study and learn how to operate Redis in Java "this article.
1. Prepare to operate 1.1 New Project
1.2 sca-jedis project relies on redis.clients jedis 3.5.2 junit junit 4.12 test com.google.code.gson gson 2.8.6 1.3 sca- Tempalte project depends on org.springframework.boot spring-boot-dependencies 2.3.2.RELEASE import pom org.springframework.boot spring-boot-starter-web Org.springframework.boot spring-boot-starter-data-redis org.springframework.boot spring-boot-starter-test test 1.4 tests whether Redispackage com.jt can be connected Import org.junit.Test;import redis.clients.jedis.Jedis;public class JedisTests {@ Test public void testGetConnection () {/ / if you can't connect, comment out bind 127.0.0.1 in redis.conf, / / and change the value of protected-mode to no, then restart redis and try Jedis jedis=new Jedis again ("192.168.126.129", 6379); / / jedis.auth ("123456") / / if the password String ping = jedis.ping (); System.out.println (ping);}} is set in redis.conf
Test result
Pay attention to consistency:
1.5 modify the redis.conf file
/ usr/local/docker/redis01/conf/ directory
You need to restart after modifying the configuration file, and then test the connection extension: set the compiled version
two。 Based on Jedis to achieve the operation of strings in redis @ Test public void testString01 () {/ / 1. Create a connection object Jedis jedis=new Jedis (ip,port); / / 2. Perform redis read and write operations / 2.1want to store string data in redis jedis.set ("id", "100s"); jedis.expire ("id", 2); jedis.set ("token", UUID.randomUUID (). ToString ()); jedis.incr ("id"); Map map=new HashMap (); map.put ("code", "2011") Map.put ("name", "redis"); Gson gson=new Gson (); String jsonStr = gson.toJson (map); / / convert the map object to the json string jedis.set ("lession", jsonStr); / / 2.2 Delete data jedis.del ("id"); / / 2.3 get data String id=jedis.get ("id") JsonStr=jedis.get ("lession"); System.out.println (id); System.out.println (jsonStr); / / 3. Release the resource jedis.close ();}
3. Pattern summary
= = share meta-pattern: = = the design idea is to reduce the number of object creation through pooling and realize the reusability of objects. All pools have the application of this design pattern.
= for example, integer pool, string pool, thread pool, connection pool
AOP proxy mode
Singleton singleton HikariPool
XxxAdapter adapter mode
Ribbon strategy
RestTemplate template method mode
SL4J facade
Interceptor execution chain mode
Factory model
4. Connection pool JedisPool applies package com.jt;import org.junit. Test;import redis.clients.jedis.Jedis;import redis.clients.jedis.JedisPool;import redis.clients.jedis.JedisPoolConfig / * sharing meta-pattern: the design idea is to reduce the number of object creation through pooling to achieve object reusability. All pooling designs have the application of this design pattern * such as integer pool, string pool, thread pool, connection pool * Jedis connection pool (connection to redis database) * * / public class JedisPoolTests {@ Test public void testJedisPool () {/ / define connection pool configuration JedisPoolConfig config=new JedisPoolConfig () / / maximum number of connections config.setMaxTotal (1000); / / maximum idle time config.setMaxIdle (60); / / create connection pool JedisPool jedisPool=new JedisPool (config, "192.168.126.129", 6379); / / get a connection from the pool Jedis resource = jedisPool.getResource () / / get data resource.set ("class", "cgb2107") through redis connection; String aClass = resource.get ("class"); System.out.println (aClass); / / release resource resource.close (); / / return the connection to jedisPool.close ();}}
The connection pool configuration can also be left unwritten because there is a default
5. Create connection pool package com.jt;import com.jt.redis.JedisDataSource;import org.junit.Test;import redis.clients.jedis.Jedis;import redis.clients.jedis.JedisPool;import redis.clients.jedis.JedisPoolConfig in singleton mode / * sharing meta-pattern: the design idea is to reduce the number of object creation through pooling and realize the reusability of objects. All pool designs have applications of this design pattern * such as integer pools, string pools, thread pools, Connection pool * Jedis connection pool (connection to redis database) * AOP proxy mode * Singleton singleton HikariPool* xxxAdapter adapter pattern * Ribbon policy * RestTemplate template method mode * SL4J facade * Interceptor execution chain mode * factory mode * * / public class JedisPoolTests {@ Test public void testJedisPool () {/ / singleton mode create connection pool Jedis resource = JedisDataSource.getConnection2 () / define connection pool configuration without writing, default / / JedisPoolConfig config=new JedisPoolConfig (); / maximum number of connections / / config.setMaxTotal (1000); / maximum idle time / / config.setMaxIdle (60); / create connection pool / / JedisPool jedisPool=new JedisPool (config, "192.168.126.129", 6379) / get a connection from the pool / / Jedis resource = jedisPool.getResource (); / / get data resource.set ("class", "cgb2107") through redis connection; String aClass = resource.get ("class"); System.out.println (aClass); / / release resource resource.close () / / return the connection to JedisDataSource.getJedisPool (). Close ();}} package com.jt.redis;import redis.clients.jedis.Jedis;import redis.clients.jedis.JedisPool;import redis.clients.jedis.JedisPoolConfig;/* singleton mode to create connection pool * / public class JedisDataSource {private static volatile JedisPool jedisPool; private static final String HOST= "192.168.126.129"; / / write to the configuration center private static final Integer PORT=6379 in the future / / static {JedisPoolConfig jedisPoolConfig=new JedisPoolConfig (); jedisPoolConfig.setMaxTotal (16); jedisPoolConfig.setMaxIdle (60); jedisPool=new JedisPool (jedisPoolConfig,HOST,PORT);} public static Jedis getConnection () {return jedisPool.getResource ();} public static JedisPool getJedisPool () {return jedisPool } / / lazy public static Jedis getConnection2 () {if (jedisPool==null) {synchronized (JedisDataSource.class) {if (jedisPool==null) {JedisPoolConfig jedisPoolConfig=new JedisPoolConfig (); jedisPoolConfig.setMaxTotal (16); jedisPoolConfig.setMaxIdle (60); jedisPool=new JedisPool (jedisPoolConfig,HOST,PORT) / / return jedisPool.getResource ();} return jedisPool.getResource ();}} extension: volatile keyword
Used to modify attributes and ensure cache consistency, but not secure
1. Ensure its thread visibility one thread changes the value, and the other threads can see it immediately
two。 Cannot guarantee its atomicity, does not guarantee thread safety, does not guarantee atomicity.
3. Disable instruction reordering (for example, count++ … )
Locking synchronized ensures atomicity
The problem of instruction reordering occurs in a multithreaded environment.
6. Project Engineering practice 6.1 distributed id
In the distributed system, when the amount of data will become larger and larger, it is necessary to divide the data into tables. However, after the table is divided, the data in each table will increase at its own pace, and there are likely to be ID conflicts. At this point, a separate mechanism is needed to generate a unique ID, which can also be called a distributed ID.
Package com.jt.redis;import redis.clients.jedis.Jedis;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;public class IdGeneratorDemo1 {public static Long getId () {/ / Jedis jedis=new Jedis ("192.168.126.129", 6379); Jedis jedis=JedisDataSource.getConnection (); Long id = jedis.incr ("id"); jedis.close (); return id } public static void main (String [] args) {/ / build thread pool ExecutorService es= Executors.newFixedThreadPool (3) with up to 3 threads; for (int iThread 1)
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.