package cn.edu.tju.service.impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import java.nio.charset.StandardCharsets;
@Service
public class RedisService {
@Autowired
private StringRedisTemplate redisTemplate;
public boolean setBitMap(String key, int index){
Boolean result = redisTemplate.opsForValue().setBit(key, index, true);
return result;
}
public String getBitMap(String key){
Long result = redisTemplate.execute(new RedisCallback<Long>() {
@Override
public Long doInRedis(RedisConnection connection) throws DataAccessException {
Long aLong = connection.bitCount(key.getBytes(StandardCharsets.UTF_8));
System.out.println("###########################");
System.out.println(aLong);
return aLong;
}
});
return String.valueOf(result);
}
}
public String bitAdd(String key1, String key2){
Long result = redisTemplate.execute(new RedisCallback<Long>() {
@Override
public Long doInRedis(RedisConnection connection) throws DataAccessException {
Long aLong = connection.bitOp(RedisStringCommands.BitOperation.AND, key1.getBytes(),
key2.getBytes());
System.out.println("###########################");
System.out.println(aLong);
return aLong;
}
});
return String.valueOf(result);
}