redis应用于缓存
程序员文章站
2022-03-22 21:22:23
...
import redis.clients.jedis.Jedis;
public class RedisJava {
private static String host = "192.168.1.101";
public static void main(String[] args) {
//isServerRunning();
// set(); //1.保存放键/值对
// setSigleList(); //2.保存List
// getRedisServerAllKeys(); //3.获取所有的key
// setSingleMap(); //4.保存Map
//setObject(); //5.保存序列化的对象
setObjectWithTimeout(); //6.保存序列化的对象并设置超时时间
//setJson(); //
}
private static void isServerRunning(){
//连接本地的 Redis 服务
Jedis jedis = new Jedis(host);
System.out.println("Connection to server sucessfully");
//查看服务是否运行
System.out.println("Server is running: "+jedis.ping());
}
static void set(){
//连接本地的 Redis 服务
Jedis jedis = new Jedis(host);
System.out.println("Connection to server sucessfully");
//设置 redis 字符串数据
jedis.set("w3ckey", "Redis tutorial");
// 获取存储的数据并输出
System.out.println("Stored string in redis:: "+ jedis.get("w3ckey"));
}
static void setSigleList(){
//连接本地的 Redis 服务
Jedis jedis = new Jedis(host);
System.out.println("Connection to server sucessfully");
//存储数据到列表中
jedis.lpush("tutorial-list", "Redis");
jedis.lpush("tutorial-list", "Mongodb");
jedis.lpush("tutorial-list", "Mysql");
jedis.lpush("tutorial-list", "Mysql2");
jedis.lpush("tutorial-list", "Mysql3");
jedis.lpush("tutorial-list", "Mysql4");
jedis.lpush("tutorial-list", "Mysql5");
// 获取存储的数据并输出
List<String> list = jedis.lrange("tutorial-list", 0 ,2);
for(int i=0; i<list.size(); i++) {
System.out.println("Stored string in redis:: "+list.get(i));
}
System.out.println();
list = jedis.lrange("tutorial-list", 1, 2);
for(int i=0; i<list.size(); i++) {
System.out.println("Stored string in redis:: "+list.get(i));
}
String val = jedis.lpop("tutorial-list"); //移除list.get(0)并返回值
System.out.println("list.get(0): "+val);
for(int i=0; i<list.size(); i++) {
System.out.println("Stored string in redis:: "+list.get(i));
}
}
static void getRedisServerAllKeys(){
//连接本地的 Redis 服务
Jedis jedis = new Jedis(host);
System.out.println("Connection to server sucessfully");
// 获取数据并输出
Set<String> list = jedis.keys("*");
String key = null;
for(Iterator it = list.iterator(); it.hasNext();) {
key = (String)it.next();
System.out.println("List of stored keys:: "+key);
}
}
static void setSingleMap(){
Jedis jedis = new Jedis(host);
Map<String, String> map = new HashMap<String, String>();
map.put("name", "fujianchao");
map.put("password", "123");
map.put("age", "12");
// 存入一个map
jedis.hmset("user", map);
// map key的个数
System.out.println("map的key的个数" + jedis.hlen("user"));
// map key
System.out.println("map的key" + jedis.hkeys("user"));
// map value
System.out.println("map的value" + jedis.hvals("user"));
// (String key, String... fields)返回值是一个list
List<String> list = jedis.hmget("user", "age", "name");
System.out.println("redis中key的各个 fields值:"
+ jedis.hmget("user", "age", "name") + list.size());
// 删除map中的某一个键 的值 password
// 当然 (key, fields) 也可以是多个fields
jedis.hdel("user", "age");
System.out.println("删除后map的key" + jedis.hkeys("user"));
jedis.hset("user", "num", "100");
System.out.println("新增后map的key" + jedis.hkeys("user"));
}
static void setObject(){
Jedis jedis = new Jedis(host);
User user = new User();
user.setName("张三");
user.setId(1000);
jedis.set("user1".getBytes(), SerializationUtil.serialize(user));
byte[] val = jedis.get("user1".getBytes());
User u1 = (User)SerializationUtil.deserialize(val);
System.out.println(u1.getName());
}
//redis缓存
static void setObjectWithTimeout(){
Jedis jedis = new Jedis(host);
User user = new User();
user.setName("张三");
user.setId(1000);
jedis.set("user1".getBytes(), SerializationUtil.serialize(user));
int timeout=2;
jedis.expire("user1".getBytes(), timeout); //2秒过期
byte[] val = jedis.get("user1".getBytes());
User u1 = (User)SerializationUtil.deserialize(val);
System.out.println(u1.getName());
try {
Thread.currentThread().sleep(3000); //阻塞3秒
} catch (InterruptedException e) {
e.printStackTrace();
}
val = jedis.get("user1".getBytes());
// u1 = (User)SerializationUtil.deserialize(val);
System.out.println(val); //超时值为空
}
}
public class SerializationUtil {
/**
* 序列化
*
* @param object
* @return
*/
public static byte[] serialize(Object object) {
ObjectOutputStream oos = null;
ByteArrayOutputStream baos = null;
try {
baos = new ByteArrayOutputStream();
oos = new ObjectOutputStream(baos);
oos.writeObject(object);
byte[] bytes = baos.toByteArray();
return bytes;
} catch (Exception e) {
}
return null;
}
/**
* 反序列化
*
* @param bytes
* @return
*/
public static Object deserialize(byte[] bytes) {
ByteArrayInputStream bais = null;
try {
bais = new ByteArrayInputStream(bytes);
ObjectInputStream ois = new ObjectInputStream(bais);
return ois.readObject();
} catch (Exception e) {
}
return null;
}
}
public class RedisJava {
private static String host = "192.168.1.101";
public static void main(String[] args) {
//isServerRunning();
// set(); //1.保存放键/值对
// setSigleList(); //2.保存List
// getRedisServerAllKeys(); //3.获取所有的key
// setSingleMap(); //4.保存Map
//setObject(); //5.保存序列化的对象
setObjectWithTimeout(); //6.保存序列化的对象并设置超时时间
//setJson(); //
}
private static void isServerRunning(){
//连接本地的 Redis 服务
Jedis jedis = new Jedis(host);
System.out.println("Connection to server sucessfully");
//查看服务是否运行
System.out.println("Server is running: "+jedis.ping());
}
static void set(){
//连接本地的 Redis 服务
Jedis jedis = new Jedis(host);
System.out.println("Connection to server sucessfully");
//设置 redis 字符串数据
jedis.set("w3ckey", "Redis tutorial");
// 获取存储的数据并输出
System.out.println("Stored string in redis:: "+ jedis.get("w3ckey"));
}
static void setSigleList(){
//连接本地的 Redis 服务
Jedis jedis = new Jedis(host);
System.out.println("Connection to server sucessfully");
//存储数据到列表中
jedis.lpush("tutorial-list", "Redis");
jedis.lpush("tutorial-list", "Mongodb");
jedis.lpush("tutorial-list", "Mysql");
jedis.lpush("tutorial-list", "Mysql2");
jedis.lpush("tutorial-list", "Mysql3");
jedis.lpush("tutorial-list", "Mysql4");
jedis.lpush("tutorial-list", "Mysql5");
// 获取存储的数据并输出
List<String> list = jedis.lrange("tutorial-list", 0 ,2);
for(int i=0; i<list.size(); i++) {
System.out.println("Stored string in redis:: "+list.get(i));
}
System.out.println();
list = jedis.lrange("tutorial-list", 1, 2);
for(int i=0; i<list.size(); i++) {
System.out.println("Stored string in redis:: "+list.get(i));
}
String val = jedis.lpop("tutorial-list"); //移除list.get(0)并返回值
System.out.println("list.get(0): "+val);
for(int i=0; i<list.size(); i++) {
System.out.println("Stored string in redis:: "+list.get(i));
}
}
static void getRedisServerAllKeys(){
//连接本地的 Redis 服务
Jedis jedis = new Jedis(host);
System.out.println("Connection to server sucessfully");
// 获取数据并输出
Set<String> list = jedis.keys("*");
String key = null;
for(Iterator it = list.iterator(); it.hasNext();) {
key = (String)it.next();
System.out.println("List of stored keys:: "+key);
}
}
static void setSingleMap(){
Jedis jedis = new Jedis(host);
Map<String, String> map = new HashMap<String, String>();
map.put("name", "fujianchao");
map.put("password", "123");
map.put("age", "12");
// 存入一个map
jedis.hmset("user", map);
// map key的个数
System.out.println("map的key的个数" + jedis.hlen("user"));
// map key
System.out.println("map的key" + jedis.hkeys("user"));
// map value
System.out.println("map的value" + jedis.hvals("user"));
// (String key, String... fields)返回值是一个list
List<String> list = jedis.hmget("user", "age", "name");
System.out.println("redis中key的各个 fields值:"
+ jedis.hmget("user", "age", "name") + list.size());
// 删除map中的某一个键 的值 password
// 当然 (key, fields) 也可以是多个fields
jedis.hdel("user", "age");
System.out.println("删除后map的key" + jedis.hkeys("user"));
jedis.hset("user", "num", "100");
System.out.println("新增后map的key" + jedis.hkeys("user"));
}
static void setObject(){
Jedis jedis = new Jedis(host);
User user = new User();
user.setName("张三");
user.setId(1000);
jedis.set("user1".getBytes(), SerializationUtil.serialize(user));
byte[] val = jedis.get("user1".getBytes());
User u1 = (User)SerializationUtil.deserialize(val);
System.out.println(u1.getName());
}
//redis缓存
static void setObjectWithTimeout(){
Jedis jedis = new Jedis(host);
User user = new User();
user.setName("张三");
user.setId(1000);
jedis.set("user1".getBytes(), SerializationUtil.serialize(user));
int timeout=2;
jedis.expire("user1".getBytes(), timeout); //2秒过期
byte[] val = jedis.get("user1".getBytes());
User u1 = (User)SerializationUtil.deserialize(val);
System.out.println(u1.getName());
try {
Thread.currentThread().sleep(3000); //阻塞3秒
} catch (InterruptedException e) {
e.printStackTrace();
}
val = jedis.get("user1".getBytes());
// u1 = (User)SerializationUtil.deserialize(val);
System.out.println(val); //超时值为空
}
}
public class SerializationUtil {
/**
* 序列化
*
* @param object
* @return
*/
public static byte[] serialize(Object object) {
ObjectOutputStream oos = null;
ByteArrayOutputStream baos = null;
try {
baos = new ByteArrayOutputStream();
oos = new ObjectOutputStream(baos);
oos.writeObject(object);
byte[] bytes = baos.toByteArray();
return bytes;
} catch (Exception e) {
}
return null;
}
/**
* 反序列化
*
* @param bytes
* @return
*/
public static Object deserialize(byte[] bytes) {
ByteArrayInputStream bais = null;
try {
bais = new ByteArrayInputStream(bytes);
ObjectInputStream ois = new ObjectInputStream(bais);
return ois.readObject();
} catch (Exception e) {
}
return null;
}
}
上一篇: redis作为dubbo服务的注册中心
下一篇: Thymeleaf在html中的输出