使用websocket编写一个聊天程序,包括上线通知和点对点聊天
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

273 lines
6.5 KiB

package com.aiprose.im.utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.data.redis.support.atomic.RedisAtomicLong;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
/**
* @author yanpeng
* @version 1.0
* @desc TODO
* @company 北京中经网软件有限公司
* @date 2022/3/3 10:35
*/
@Component
public class RedisUtil {
@Autowired
private RedisTemplate redisTemplate;
@Resource(name="redisTemplate")
private ValueOperations<String, Object> valueOperations;
@Resource(name="redisTemplate")
private HashOperations<String, String, Object> hashOperations;
@Resource(name="redisTemplate")
private ListOperations<Object, Object> listOperations;
/**
* @param key
* @param value
* @param expireTime
* @Title: set
* @Description: set cache.
*/
public void set(String key, int value, Date expireTime) {
RedisAtomicLong counter = new RedisAtomicLong(key, redisTemplate.getConnectionFactory());
counter.set(value);
counter.expireAt(expireTime);
}
/**
* @param key
* @param value
* @param timeout
* @param unit
* @Title: set
* @Description: set cache.
*/
public void set(String key, int value, long timeout, TimeUnit unit) {
RedisAtomicLong counter = new RedisAtomicLong(key, redisTemplate.getConnectionFactory());
counter.set(value);
counter.expire(timeout, unit);
}
/**
* @param key
* @return
* @Title: generate
* @Description: Atomically increments by one the current value.
*/
public long generate(String key) {
RedisAtomicLong counter = new RedisAtomicLong(key, redisTemplate.getConnectionFactory());
return counter.incrementAndGet();
}
/**
* @param key
* @return
* @Title: generate
* @Description: Atomically increments by one the current value.
*/
public long generate(String key, Date expireTime) {
RedisAtomicLong counter = new RedisAtomicLong(key, redisTemplate.getConnectionFactory());
counter.expireAt(expireTime);
return counter.incrementAndGet();
}
/**
* @param key
* @param increment
* @return
* @Title: generate
* @Description: Atomically adds the given value to the current value.
*/
public long generate(String key, int increment) {
RedisAtomicLong counter = new RedisAtomicLong(key, redisTemplate.getConnectionFactory());
return counter.addAndGet(increment);
}
/**
* @param key
* @param increment
* @param expireTime
* @return
* @Title: generate
* @Description: Atomically adds the given value to the current value.
*/
public long generate(String key, int increment, Date expireTime) {
RedisAtomicLong counter = new RedisAtomicLong(key, redisTemplate.getConnectionFactory());
counter.expireAt(expireTime);
return counter.addAndGet(increment);
}
/**
* 保存对象
* @param key
* @param value
*/
public void setObject(String key,Object value) {
valueOperations.set(key, value);
}
/**
* 获取对象
* @param key
* @return
*/
public Object getObect(String key) {
return valueOperations.get(key);
}
/**
* 删除对象
* @param key
*/
public void deleteObject(String key) {
redisTemplate.delete(key);
}
/**
* 插入HaspMap
* @param key
* @param hashKey
* @param value
*/
public void setHash(String key, String hashKey, Object value) {
hashOperations.put(key, hashKey, value);
}
/**
* 获取知道下标HaspMap
* @param key
* @param hashKey
* @return
*/
public Object getHashIndex(String key, String hashKey) {
return hashOperations.get(key, hashKey);
}
/**
* 获取HaspMap
* @param key
* @return
*/
public List<Object> getHash(String key) {
return hashOperations.values(key);
}
/**
* 判断是否存在某个key
* @param key
* @param hashKey
* @return
*/
public Boolean hasKeyHash(String key, String hashKey) {
return hashOperations.hasKey(key, hashKey);
}
/**
* 获取Map
* @param key
* @return
*/
public Map<String, Object> entriesHasp(String key){
return hashOperations.entries(key);
}
/**
* 获取Map
* @param key
* @return
*/
public Set<String> hashKeys(String key){
return hashOperations.keys(key);
}
/**
* 大小
* @param key
* @return
*/
public long sizeHash(String key) {
return hashOperations.size(key);
}
/**
* 删除HaspMap
* @param key
* @param value
*/
public void deleteHash(String key, Object value) {
hashOperations.delete(key, value);
}
/**
* 数组里面添加元素
* @param key
* @param value
*/
public void setLeftList(String key,Object value)
{
listOperations.leftPush(key, value);
}
/**
* 全部添加
* @param key
* @param list
*/
public void setleftAllList(String key, List list) {
listOperations.leftPushAll(key, list);
}
/**
* 对指定下标的数组元素进行替换
* @param key
* @param index
* @param value
*/
public void setList(String key,long index,Object value) {
listOperations.set(key, index, value);
}
/**
* 数组大小
* @param key
*/
public long sizeList(String key) {
return listOperations.size(key);
}
/**
* 获取指定下标元素
* @param key
* @param index
* @return
*/
public Object getListIndex(String key,long index) {
return listOperations.index(key, index);
}
/**
* 获取list 指定开始-结束
* @param key
* @param start 开始
* @param end 结束
* @return
*/
public Object getList(String key,long start, long end) {
return listOperations.range(key, start, end);
}
}