首先看一下java如何链接redis
Maven:
redis.clients
jedis
3.0.1
Java:
public static void main(String[] args) {
Jedis jedis = new Jedis("192.168.5.137",6379);//链接redis,参数为IP和Port
Set keys = jedis.keys("*"); //查看redis已存在的key
for(String key : keys){
System.out.println(key);
}
}
Java链接redis用到的类是Jedis,并通过Jedis,操作Redis中的数据。下面就来看一下Jedis中提供的函数。
Jedis构造中提供了丰富的redis链接方式和参数设置,其中最常用的也就是前面几个,设置redis的IP和Port,再就是设置链接超时时间和是否使用SSL(安全套接字层)
public Jedis() { }
public Jedis(String host) { super(host); }
public Jedis(HostAndPort hp) { super(hp); } //HostAndPort中封装了host和port信息
public Jedis(String host, int port) { super(host, port); }
public Jedis(String host, int port, boolean ssl) { super(host, port, ssl); }
public Jedis(String host, int port, int timeout) { super(host, port, timeout); }
public Jedis(String host, int port, int timeout, boolean ssl) {
super(host, port, timeout, ssl);
}
public Jedis(String host, int port, boolean ssl, SSLSocketFactory sslSocketFactory, SSLParameters sslParameters, HostnameVerifier hostnameVerifier) {
super(host, port, ssl, sslSocketFactory, sslParameters, hostnameVerifier);
}
public Jedis(String host, int port, int timeout, boolean ssl, SSLSocketFactory sslSocketFactory, SSLParameters sslParameters, HostnameVerifier hostnameVerifier) {
super(host, port, timeout, ssl, sslSocketFactory, sslParameters, hostnameVerifier);
}
public Jedis(String host, int port, int connectionTimeout, int soTimeout) {
super(host, port, connectionTimeout, soTimeout);
}
public Jedis(String host, int port, int connectionTimeout, int soTimeout, boolean ssl) {
super(host, port, connectionTimeout, soTimeout, ssl);
}
public Jedis(String host, int port, int connectionTimeout, int soTimeout, boolean ssl, SSLSocketFactory sslSocketFactory, SSLParameters sslParameters, HostnameVerifier hostnameVerifier) {
super(host, port, connectionTimeout, soTimeout, ssl, sslSocketFactory, sslParameters, hostnameVerifier);
}
public Jedis(JedisShardInfo shardInfo) { super(shardInfo); }
public Jedis(URI uri) { super(uri); }
public Jedis(URI uri, int timeout) { super(uri, timeout); }
public Jedis(URI uri, SSLSocketFactory sslSocketFactory, SSLParameters sslParameters, HostnameVerifier hostnameVerifier) {
super(uri, sslSocketFactory, sslParameters, hostnameVerifier);
}
public Jedis(URI uri, int timeout, SSLSocketFactory sslSocketFactory, SSLParameters sslParameters, HostnameVerifier hostnameVerifier) {
super(uri, timeout, sslSocketFactory, sslParameters, hostnameVerifier);
}
public Jedis(URI uri, int connectionTimeout, int soTimeout) {
super(uri, connectionTimeout, soTimeout);
}
public Jedis(URI uri, int connectionTimeout, int soTimeout, SSLSocketFactory sslSocketFactory, SSLParameters sslParameters, HostnameVerifier hostnameVerifier) {
super(uri, connectionTimeout, soTimeout, sslSocketFactory, sslParameters, hostnameVerifier);
}
Jedis包含了所有对redis的操作函数,且命令函数与我们直接在redis客户端中对redis的操作命令非常相近,下面我们列举部分redis中的函数供大家参考,更多的Jedis API大家可以看一下Jedis的源码。这里大家可以对照redis常用命令来学习。
Boolean exists(String var1);
Long persist(String var1);
Set keys(String var1);
String select(int index)
Long move(String key, int dbIndex)
String set(String var1, String var2);
String get(String var1);
String getSet(String var1, String var2);
List mget(String... var1);
String mset(String... var1);
List blpop(int var1, String... var2);
List brpop(int var1, String... var2);
List blpop(String... var1);
List brpop(String... var1);
String lpop(String var1);
String rpop(String var1);
Long sadd(String var1, String... var2);
Set smembers(String var1);
Long srem(String var1, String... var2);
String spop(String var1);
Set spop(String var1, long var2);
Long scard(String var1);
Long zadd(String var1, double var2, String var4);
Long zadd(String var1, Map var2);
Long zcard(String var1);
Long zcount(String var1, double var2, double var4);
Long zcount(String var1, String var2, String var3);
Set zrangeByScore(String var1, double var2, double var4);
Set zrangeByScore(String var1, String var2, String var3);
Boolean hexists(String var1, String var2);
Long hdel(String var1, String... var2);
Long hlen(String var1);
Set hkeys(String var1);
List hvals(String var1);