首页 技术 正文
技术 2022年11月20日
0 收藏 931 点赞 3,975 浏览 2417 个字
public class WnsRedisFactory {              private static Cache pool = null;
private static JedisConnectionFactory redisConnFactory = null; //对主缓存 test的操作
private static Cache getInstance() {
if (pool == null) {
pool = Redis.use("test");
}
return pool;
}
/**
* 通过key删除
*
* @param key
*/
public static void del(String key) throws Exception {
Cache redis = null; redis = getInstance();
redis.del(key); } public static void del(String key, String field) throws Exception {
Cache redis = null; redis = getInstance();
redis.hdel(key, field); }
/**
* 添加key value 并且设置存活时间
*
* @param key
* @param value
* @param liveTime
*/
public static void set(String key, String value, int liveTime) throws Exception {
Cache redis = null; redis = getInstance();
Jedis jedis =redis.getJedis();
jedis.set(key, value);
redis.expire(key, liveTime);
redis.close(jedis); } public static Long decrby(String key, int value) throws Exception {
Cache redis = null; redis = getInstance();
return redis.decrBy(key, value); } public static Long decr(String key) throws Exception {
Cache redis = null; redis = getInstance();
return redis.decr(key); }
/**
* 设置或者清除指定key的value上的某个位置的比特位,如果该key原先不存在,则新创建一个key,其value将会自动分配内存,
* 直到可以放下指定位置的bit值。
*
* @param key
* @param offset
* @param value true代表1,false代表0
* @return
* @return 返回原来位置的bit值是否是1,如果是1,则返回true,否则返回false。
*/
public static void setbit(String key,Long offset ,Boolean value,int liveTime)
{
Cache redis = null; redis = getInstance();
Jedis jedis = redis.getJedis();
try {
jedis.setbit(key, offset, value);
}
finally {redis.close(jedis);}
redis.expire(key, liveTime); } //计算传人二进制字符串 1的个数
public static long bitcount(String key)
{
Cache redis = null; redis = getInstance();
Jedis jedis = redis.getJedis();
try {
return jedis.bitcount(key);
}
finally {redis.close(jedis);} } //通过传人的op(and/or)将传人的二进制 与/非 传回结果
public static long bitop(BitOP op,String destKey,String ... srcKeys)
{
Cache redis = null;
redis = getInstance(); Jedis jedis = redis.getJedis();
long result = 0; result = jedis.bitop(op, destKey, srcKeys);
redis.close(jedis);
return result;
}
/**
* bitMap进行and运算
* RedisKeys.DAYBITOPDESTKEY 为静态变量
*/
// 换一种实现方式,不用 bitset 类,直接用redis的bit操作
public static int bitMapAnd( String...keys){
int result = 0;
BitOP op = BitOP.AND; try {
bitop(op, RedisKeys.DAYBITOPDESTKEY, keys);
result = (int) WnsRedisFactory.bitcount(RedisKeys.DAYBITOPDESTKEY); return result; } catch (Exception e) {
System.out.println("AND算法异常");
}
return 0;
} /**
* bitMap进行OR运算
*/
// 换一种实现方式,不用 bitset 类,直接用redis的bit操作
public static int bitMapOr( String...keys){
int result = 0;
BitOP op = BitOP.OR; try {
bitop(op, RedisKeys.DAYBITOPDESTKEY, keys);
result = (int) WnsRedisFactory.bitcount(RedisKeys.DAYBITOPDESTKEY); return result;
} catch (Exception e) {
System.out.println("OR算法异常");
}
return 0;
}
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,031
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,520
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,368
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,148
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,781
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,860