Java Redis and JavaRedis
Redis is an open-source, advanced key-value storage solution that can be used to build high-performance, scalable Web applications.
The official Redis website is http://www.redis.io/, as shown below:
Redis has three characteristics that distinguish it from many other competitors:
- Redis is a database that stores data completely in the memory and uses disks for persistence purposes only;
- Compared with many key-value data storage systems, Redis has relatively rich data types;
- Redis can copy data to any number of slave servers;
Advantages of Redis
Exception fast: Redis is very fast. It can perform about 110000 setting operations per second and 81000 read operations per second.
Supports a wide range of data types: Redis supports data types that most developers already know, such as list, set, sortedset, and hash.
This makes it easy to solve various problems in applications, because we know which types of data are used to better solve the problems.
Operations are atomic: All Redis operations are atomic, so that when two customers access the Redis server at the same time, they get the updated value (the latest value ).
- MultiUtility tool: Redis is a versatile utility that can be used in many applications, such as cache and message passing queues (Redis native supports publishing/subscription). For example: web application sessions, website page clicks, and other transient data;
To install redis in windows:
- Download the redis package from the official website.
- Run the cmd command to find the directory where redis is stored and start the server,
Then, the client can directly connect to the client through the cmd command,
This figure is used to connect to the local redis and set similar to "myKey", "test" in the map set. And then get it through the get method.
After redis is introduced and the startup server and connection are installed, the following describes how to use it in Java. This is also a popular technology:
- First in java using Redis, need to introduce Redis driver, is the jedis-2.1.0.jar, and this jar package depends on the commons-pool-1.6.jar and tomcat-redis-session-manager-1.2-tomcat-7.jar, so we need at least three jar packages.
- The second step is to start the local redis server.
- Then connect to the local redis instance through the driver in Java, as shown below: Jedis jedis = new Jedis ("localhost ");
- At the same time, to test the connection success, you can insert the keyvlaue value as in the command, for example, jedis. set ("myKey", "succeeded"); then the console outputs the obtained value: System. out. println ("Redis key value:" + jedis. get ("myKey "));
This simple redis can be used in Java. For more information, see Chinese apihttp: // www.360doc.com/content/15/0328/12/5054188_458684113.shtml.
Ps: the advantage of redis is that it does not need to frequently access the database. When some data does not change, it can be directly added to redis, such as chatting with some historical records, similar to caching, this greatly improves the performance. I suggest using redis in combination with databases. The following is a piece of code written by the author in the small test function that combines redis and database login:
Int user_id = 2; // user ID
String user_name; // User Name
If (jedis. hexists ("user" + user_id, "user_name") {// determines whether the "user" + user_id hash table exists in jedis and the value is user_name
User_name = jedis. hget ("user" + user_id, "user_name"); // you can directly obtain and log on
System. out. println ("Welcome to Redis:" + user_name );
} Else {// If no, you need to query the database to check whether the user name and password are correct. If yes, log in and save the information to redis.
String SQL = "select user_name from user_t where id =" + user_id + "";
Try {
ResultSet rs restart conn.createstatement(cmd.exe cuteQuery (SQL );
If (rs. next ()){
Jedis. hset ("user" + user_id, "user_name", rs. getString ("user_name"); // save to redis
System. out. println ("Welcome to MySql:" + rs. getString ("user_name "));
} Else {
System. out. println ("Welcome to register ");
}
} Catch (SQLException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
Here is a complete simple introduction to redis. Welcome to learn from each other.