当前位置:网站首页>Basic knowledge and practical application of redis
Basic knowledge and practical application of redis
2022-06-22 08:22:00 【Hanxiaozhi】
One : Non relational database
Two :Redis Supported data types
- String type string( Commonly used :json/xml)( cache )
- Hash type hash(key–value)map
- List the type list
- Collection types set
- Ordered set type sortedset( Ranking List )
3、 ... and :Redis Application scenarios
1、 cache ( Data query ( High concurrency and long-term fixed data ), End link , News content , Product content and so on );– Most used
2、 A list of online friends in a chat room ;
3、 Task queue ;( seckill , Rush purchase ,12306 wait )
4、 Application ranking ( Microwave thermal search );
5、 Website visit statistics ;
6、 Data expiration processing ( Send verification code );
7、 Distributed lock ;
Four :window edition Redis Installation and use of
1.window edition Redis Download the official website
2.window edition Redis Directory structure of
3.window Version of Redis It's installation free , After decompressing the compressed package, you can use .
4.window edition Redis Start and close ( Server side )
①: start-up : double-click redis-server.exe
②: close :Ctrl+C perhaps Close the service window
5. Connect Redis service ( client )
①: double-click redis-cli.exe file – Command line tools
②: Graphical interface tools
6. Graphical software installation ( client )– Later, use the graphical interface to operate
①: name
②:IP
③: Port number
5、 ... and :Redis Data structure of
1.Redis It's a kind of advanced Key-Value Type of storage system , among key The value is a string type ,value Value support 5 Common data types .
6、 ... and : Different data types ( Add / delete query command )
7、 ... and : Knowledge of general commands
8、 ... and :Jedis Basic use of –java Language client
1.jedis It's the official first choice java Client development package ( Equate to Redis desktop manager Graphical interface client – Operation command interface )
2.jedis The method of adding, deleting, and querying is
3.java Code demo Realization
Nine :jedis Knowledge of connection pool
1.Jedis example :
public class StudentService {
StudentDao studentDao = new StudentDao();
/** * Check all student information * @return */
public String queryAllStudent() {
// First from redis In order to get
Jedis jedis = JedisUtil.getJedis();
long redisBegin = System.currentTimeMillis();
String stusData = jedis.get("stusData");
if(null!=stusData){
long redisEnd = System.currentTimeMillis();
System.out.println(" from redis Time consuming for query in :"+(redisEnd-redisBegin));
}
// To query from a database
if (null == stusData) {
long begin = System.currentTimeMillis();
List<Student> studentList = studentDao.queryAll();
long end = System.currentTimeMillis();
System.out.println(" Querying from the database takes time :"+(end-begin));
// Turn data into json character string
stusData = JSON.toJSONString(studentList);
// Save the data to redis in
jedis.set("stusData", stusData);
}
// Release resources
jedis.close();
return stusData;
}
}
Ten :Spring Data Redis(spring Packaged Redis)– It is more convenient to use this in the future Jedis– important
1.Spring Data by Redis A tool class is provided :RedisTemplate
2. Its usage 3 step :
①: Introduce dependencies
②:yml perhaps properties Configure configuration parameters in (ip, port , Connection pool parameters …)
③: Inject RedisTemplate object , And the method of object calling stored value and value
3.RedisTemplate It encapsulates for Redis Various operations of five data structures
redisTemplate.opsForValue() : Operation string
redisTemplate.opsForHash() : operation hash
redisTemplate.opsForList(): operation list
redisTemplate.opsForSet(): operation set
redisTemplate.opsForZSet(): operation zset
example :
public void sendCode(String phone) {
// Verify phone number format
if (!RegexUtils.isPhone(phone)) {
throw new LyException(ExceptionEnum.INVALID_PHONE_NUMBER);
}
// Generate verification code
String code = RandomStringUtils.randomNumeric(6);
// Save the verification code to redis
redisTemplate.opsForValue().set(KEY_PREFIX + phone, code, 1, TimeUnit.MINUTES);
// send out RabbitMQ The message to ly-sms
Map<String, String> msg = new HashMap<>();
msg.put("phone", phone);
msg.put("code", code);
amqpTemplate.convertAndSend(SMS_EXCHANGE_NAME, VERIFY_CODE_KEY, msg);
}
边栏推荐
- Concatenate the specified character at the end of a number in a string
- Mt4/mql4 getting started to mastering EA tutorial lesson 3 - common functions of MQL language (III) - common functions of K-line value taking
- MySQL queries data within one hour
- QT QtableView的使用示例
- Multi tenancy and Implementation
- Example of QT combox
- Collections and arrays
- Summary of sub database and sub table 2
- 并发线程池底层原理详解与源码分析
- Develop steam education based on scientific skills
猜你喜欢

2022年CIO面临的七大挑战及应对方法

Implementation and landing of any to any real-time voice change RTC dev Meetup

QT error prompt 1:invalid use of incomplete type '***‘

Some mathematical operation functions in LabVIEW

Mt4/mql4 getting started to mastering EA tutorial lesson 5 - common functions of MQL language (V) - common functions of account information

QT custom composite control (class promotion function)

Installing SQL Server 2008 by image in Windows Service 2008

多点闹钟实例

【 Oracle database】 Nursery Mother Tutorial day13 date Function

Bee framework, an ORM framework that can be learned in ten minutes --bee
随机推荐
JSON使用示例
Some suggestions on Oracle SQL query return optimization
2022年CIO面临的七大挑战及应对方法
dom4j+xpath解析xml文件
Oracle execution plan analysis
并发三大特性2-有序性
学科融合对steam教育的作用
The jdbcurl is configured correctly in the project, but the jdbcurl is the wrong path after the project is started
Using KDJ metrics on MT4
The solution to the problem of the first screen picture loading flicker
Any to Any 实时变声的实现与落地丨RTC Dev Meetup
C # realizes voice reading function
先锋期货安全么?期货开户都是哪些流程?期货手续费怎么降低?
安装 MySQL 服务时提示 InstallRemove of the Service Denied
[Oracle database] mammy tutorial day14 conversion function
Nisp online simulation question bank
并发线程池底层原理详解与源码分析
Define the data source of hikaricp connection pool for bee
C# 接口持有结构体会导致装箱问题
成为一名卓越云架构师要具备的五项技能