What are the common API operations of redis?
//设置键值对 //set key value [EX seconds] [PX milliseconds] [NX|XX] //获取值 //get key //删除键 //del key //清空数据库 //flushdb //获取list值 //lrange key start stop //map类型 //hget key field //hgetall key //sortedset类型 package hgs.redislearn; import java.io.IOException; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Set; import org.codehaus.jackson.JsonGenerationException; import org.codehaus.jackson.map.JsonMappingException; import org.codehaus.jackson.map.ObjectMapper; import org.junit.After; import org.junit.Before; import org.junit.Test; import redis.clients.jedis.BinaryClient.LIST_POSITION; import redis.clients.jedis.Jedis; /** * * <p>Description:Redis </p> * @author guangshihao * @date 2018年9月28日 * */ public class RedisMainTest { Jedis jedis = null; @Before public void getConnect() { jedis = new Jedis("192.168.6.129", 6379); } @Test public void sysoutConnect() { System.out.println(jedis.ping()); } @After public void finaly() { jedis.close(); } //测试放入String类型的键值对,并进行获取删除修改等操作 @Test public void TestStringKeys() { jedis.set("hgs.name", "haoguangshi."); String name = jedis.get("hgs.name"); System.out.println(name); long affect = jedis.del("hgs.name"); System.out.println(affect); } @Test public void TestListKeys() { //从左边插入 jedis.lpush("test_list", "1","2","100"); //从右边插入 jedis.rpush("test_list", "100","2","1","this"); //获取list List<String > lis = jedis.lrange("test_list", 0, -1); System.out.println(lis); //jedis.lpop lpush rpop rpush //在this出现的第一个位置的前面插入is jedis.linsert("test_list", LIST_POSITION.BEFORE, "this", "is"); } //map类型操作 @Test public void TestMapKeys() throws JsonGenerationException, JsonMappingException, IOException { /* Person p = new Person (); p.setName("hgs"); p.setAge(26); p.setWeight(65); Person p1 = new Person (); p1.setName("wd"); p1.setAge(23); p1.setWeight(60); ObjectMapper mapper = new ObjectMapper(); String sp = mapper.writer().writeValueAsString(p); String sp1 = mapper.writer().writeValueAsString(p1); Map<String,String> ps = new HashMap<String, String>(); ps.put("hgs", sp); ps.put("wd", sp1);*/ jedis.hset("pseron:hgs", "name", "hgs"); jedis.hset("pseron:hgs", "age", "24"); jedis.hset("pseron:hgs", "weigth", "65kg"); jedis.hset("pseron:wd", "name", "wd"); jedis.hset("pseron:wd", "age", "24"); jedis.hset("pseron:wd", "weight", "60kg"); } @Test public void loopMapKeys() { //遍历一个map Map<String,String> wd = jedis.hgetAll("pseron:wd"); for(String type : wd.keySet()) { System.out.println(type+" : " +wd.get(type)); } //原子操作自增2 jedis.hincrBy("pseron:hgs\"", "age", 2L); } @Test public void delMapKeys() { //删除属性 jedis.hdel("pseron:wd", "weight"); //打印集合的长度 System.out.println(jedis.hlen("pseron:hgs")); } //set类型数据结构,添加数据 @Test public void setKeysTest() { String[] tmp = new String[] { "tianmao","dingding","alimama","zhifubao","feizhu" }; jedis.sadd("apps", tmp); String[] tmp1 = new String[] { "tianmao","dingdi","alima","zhifubao","feizhu" }; jedis.sadd("apps", tmp); jedis.sadd("apps1", tmp1); } //遍历 @Test public void scanSetKeys() { //判断某个记录是否存在 System.out.println(jedis.sismember("apps", "tianmao")); System.out.println(jedis.scard("apps")); //计算交集 Set<String> sets = jedis.sinter("apps","apps1"); for(String s : sets) { System.out.println(s); } //并集 Set<String> sets1 = jedis.sunion("apps","apps1"); for(String s : sets1) { System.out.println(s); } System.out.println(); //差集 Set<String> sets2 = jedis.sdiff("apps","apps1"); for(String s : sets2) { System.out.println(s); } //遍历 Set<String> sets3 = jedis.smembers("apps"); for(String s : sets3) { System.out.println(s); } } //SortedSet数据类型 //存数据 @Test public void sortedSetAddTest() { Map<String,Double> scoremem = new HashMap<String, Double>(); scoremem.put("wd", (double) 88); scoremem.put("cm", (double) 87); scoremem.put("zz", (double) 90); scoremem.put("wzf", (double) 70); scoremem.put("xzh", (double) 66); scoremem.put("hgs", (double) 55); scoremem.put("hjh", (double) 88); scoremem.put("shk", (double) 100); jedis.zadd("roommeets_score", scoremem); } //sortedSet 遍历 @Test public void scanSortedSet() { //正序遍历 Set<String> members = jedis.zrange("roommeets_score", 0, -1); for(String mem : members) { System.out.println("name:"+mem +","+" score:" +jedis.zscore("roommeets_score", mem)+","+" rank:"+(jedis.zrank("roommeets_score", mem)+1)); } System.out.println(); //正序遍历 Set<String> members1 = jedis.zrevrange("roommeets_score", 0, -1); for(String mem : members1) { System.out.println("name:"+mem +","+" score:" +jedis.zscore("roommeets_score", mem)+","+" rank:"+(jedis.zrevrank("roommeets_score", mem)+1L)); } } } class Person{ String name ; int age; double weight; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public double getWeight() { return weight; } public void setWeight(double weight) { this.weight = weight; } @Override public String toString() { return "Person [name=" + name + ", age=" + age + ", weight=" + weight + "]"; } }
The above is the detailed content of What are the common API operations of redis?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Redis cluster mode deploys Redis instances to multiple servers through sharding, improving scalability and availability. The construction steps are as follows: Create odd Redis instances with different ports; Create 3 sentinel instances, monitor Redis instances and failover; configure sentinel configuration files, add monitoring Redis instance information and failover settings; configure Redis instance configuration files, enable cluster mode and specify the cluster information file path; create nodes.conf file, containing information of each Redis instance; start the cluster, execute the create command to create a cluster and specify the number of replicas; log in to the cluster to execute the CLUSTER INFO command to verify the cluster status; make

How to clear Redis data: Use the FLUSHALL command to clear all key values. Use the FLUSHDB command to clear the key value of the currently selected database. Use SELECT to switch databases, and then use FLUSHDB to clear multiple databases. Use the DEL command to delete a specific key. Use the redis-cli tool to clear the data.

To read a queue from Redis, you need to get the queue name, read the elements using the LPOP command, and process the empty queue. The specific steps are as follows: Get the queue name: name it with the prefix of "queue:" such as "queue:my-queue". Use the LPOP command: Eject the element from the head of the queue and return its value, such as LPOP queue:my-queue. Processing empty queues: If the queue is empty, LPOP returns nil, and you can check whether the queue exists before reading the element.

Using the Redis directive requires the following steps: Open the Redis client. Enter the command (verb key value). Provides the required parameters (varies from instruction to instruction). Press Enter to execute the command. Redis returns a response indicating the result of the operation (usually OK or -ERR).

Using Redis to lock operations requires obtaining the lock through the SETNX command, and then using the EXPIRE command to set the expiration time. The specific steps are: (1) Use the SETNX command to try to set a key-value pair; (2) Use the EXPIRE command to set the expiration time for the lock; (3) Use the DEL command to delete the lock when the lock is no longer needed.

The best way to understand Redis source code is to go step by step: get familiar with the basics of Redis. Select a specific module or function as the starting point. Start with the entry point of the module or function and view the code line by line. View the code through the function call chain. Be familiar with the underlying data structures used by Redis. Identify the algorithm used by Redis.

Redis data loss causes include memory failures, power outages, human errors, and hardware failures. The solutions are: 1. Store data to disk with RDB or AOF persistence; 2. Copy to multiple servers for high availability; 3. HA with Redis Sentinel or Redis Cluster; 4. Create snapshots to back up data; 5. Implement best practices such as persistence, replication, snapshots, monitoring, and security measures.

Use the Redis command line tool (redis-cli) to manage and operate Redis through the following steps: Connect to the server, specify the address and port. Send commands to the server using the command name and parameters. Use the HELP command to view help information for a specific command. Use the QUIT command to exit the command line tool.
