https://www.tutorialspoint.com/redis/redis_java.htm
import redis.clients.jedis.Jedis;
public class RedisJava {
public static void main(String[] args) {
//Connecting to Redis server on localhost
Jedis jedis = new Jedis("localhost");
System.out.println("Connection to server sucessfully");
//check whether server is running or not
System.out.println("Server is running: "+jedis.ping());
}
}
[popexizhi:
mvn pom.xml 依赖如下:
redis.clients jedis 2.8.1
可用版本参考:https://mvnrepository.com/artifact/redis.clients/jedis
]
________________________________________________________________________________________
-----------------------------------------------------
[popexizhi:1. redis 本地配置文件使用支持0.0.0.0的监听,默认启动使用的是127.0.0.12. redis-cli查看命令中 list 类型支持python的-1索引方式eg:LRANGE hi 0 -1
]
]
-----------------------------------------------------
其他类型数据参见: http://www.baeldung.com/jedis-java-redis-client-library
5.1. Strings
Strings are the most basic kind of Redis value, useful for when you need to persist simple key-value data types:
12jedis.set("events/city/rome","32,15,223,828");String cachedResponse = jedis.get("events/city/rome");The variable cachedResponse will hold the value 32,15,223,828. Coupled with expiration support, discussed later, it can work as a lightning fast and simple to use cache layer for HTTP requests received at your web application and other caching requirements.5.2. Lists
Redis Lists are simply lists of strings, sorted by insertion order and make it an ideal tool to implement, for instance, message queues:
1234jedis.lpush("queue#tasks","firstTask");jedis.lpush("queue#tasks","secondTask");String task = jedis.rpop("queue#tasks");The variable task will hold the value firstTask. Remember that you can serialize any object and persist it as a string, so messages in the queue can carry more complex data when required.5.3. Sets
Redis Sets are an unordered collection of Strings that come in handy when you want to exclude repeated members:
123456jedis.sadd("nicknames","nickname#1");jedis.sadd("nicknames","nickname#2");jedis.sadd("nicknames","nickname#1");Setnicknames = jedis.smembers( "nicknames");booleanexists = jedis.sismember("nicknames","nickname#1");The Java Set nicknames will have a size of 2, the second addition of nickname#1 was ignored. Also, the existsvariable will have a value of true, the method sismember enables you to quickly check for the existence of a particular member.5.4. Hashes
Redis Hashes are mapping between String fields and String values:
1234567jedis.hset("user#1","name","Peter");jedis.hset("user#1","job","politician");String name = jedis.hget("user#1","name");Mapfields = jedis.hgetAll( "user#1");String job = fields.get("job");As you can see, hashes are a very convenient data type when you want to access object’s properties individually since you do not need to retrieve the whole object.
没有评论:
发表评论