更换了Redis的库

新增了对机器人的操作(redis)
This commit is contained in:
2020-05-27 14:40:25 +08:00
parent 2a06117ec7
commit d247235ca5
10 changed files with 239 additions and 137 deletions

View File

@@ -1,33 +1,137 @@
package com.yutou.tools.utils;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.util.Set;
import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;
import redis.clients.jedis.Jedis;
@Component
public class RedisTools {
@Resource
RedisTemplate<String, String> redisTemplate;
private static String host;
private static int port;
private static int TOKEN_TIMEOUT_DEFAULT=360;
private RedisTools() {
public void set(String key, String value) {
redisTemplate.opsForValue().set(key, value);
}
public void set(String key, String value, long time) {
System.out.println("key=" + key + " value=" + value + " time=" + time);
redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS);
// 写成静态代码块形式,只加载一次,节省资源
static {
//Properties properties = PropertyUtil.loadProperties("jedis.properties");
//host = properties.getProperty("redis.host");
//port = Integer.valueOf(properties.getProperty("redis.port"));
host="127.0.0.1";
port=6379;
}
public String get(String key) {
public static boolean set(int dbIndex,String key,String value){
try {
return redisTemplate.opsForValue().get(key);
Jedis jedis =getRedis();
jedis.select(dbIndex);
String ret=jedis.set(key,value);
System.out.println("Redis set ="+ret);
jedis.close();
} catch (Exception e) {
return null;
// TODO: handle exception
e.printStackTrace();
return false;
}
return true;
}
public static boolean set(String key, String value) {
return set(0,key,value);
}
public static boolean set(String key,String value,int timeout) {
try {
Jedis jedis=getRedis();
if(timeout==-1){
jedis.set(key,value);
}else {
jedis.setex(key, timeout, value);
}
jedis.close();
}catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
return false;
}
return true;
}
public static String get(String key,int dbIndex){
String value = "-999";
Jedis jedis=null;
try {
jedis = getRedis();
jedis.select(dbIndex);
value = jedis.get(key);
jedis.close();
} catch (Exception e) {
// TODO: handle exception
// e.printStackTrace();
}finally {
if(jedis!=null)
jedis.close();
}
return value;
}
public static String get(String key) {
return get(key,0);
}
public static boolean remove(String key) {
Jedis jedis=getRedis();
Long i=jedis.del(key);
jedis.close();
if(i>0) {
return true;
}else {
return false;
}
}
public static void removeLoginState(String uid) {
Jedis jedis=getRedis();
Set<String> keys=jedis.keys("*");
for (String string : keys) {
if(string.equals(uid)) {
jedis.del(string);
}
}
}
public static String ping() {
Jedis jedis=getRedis();
String tmp=jedis.ping();
jedis.close();
return tmp;
}
public static boolean exists(String key,String value) {
Jedis jedis=getRedis();
boolean flag=value.equals(jedis.get(key));
jedis.close();
return flag;
}
public static Jedis getRedis() {
return new Jedis(host,port);
}
private static class PropertyUtil {
// 加载property文件到io流里面
public static Properties loadProperties(String propertyFile) {
Properties properties = new Properties();
try {
InputStream is = PropertyUtil.class.getClassLoader().getResourceAsStream(propertyFile);
if (is == null) {
is = PropertyUtil.class.getClassLoader().getResourceAsStream(propertyFile);
}
properties.load(is);
} catch (IOException e) {
e.printStackTrace();
}
return properties;
}
}
public static void main(String[] args) {
String str= "\"aaa\"";
str=str.replaceAll("\"aaa\"", "0");
}
}