import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import org.springframework.data.redis.connection.RedisPassword;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.SerializationException;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import com.caucho.hessian.io.Hessian2Input;
import com.caucho.hessian.io.Hessian2Output;
import com.caucho.hessian.io.JavaSerializer;
import com.caucho.hessian.io.Serializer;
import com.caucho.hessian.io.SerializerFactory;
import com.google.common.base.Charsets;
import com.google.common.base.Suppliers;
import com.google.common.hash.BloomFilter;
import com.google.common.hash.Funnel;
import com.google.common.hash.PrimitiveSink;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class bloom {
private static RedisTemplate stringRedisTemplate = Suppliers.memoize(
() -> initRedis()).get();
private static Serializer serializer = JavaSerializer.create(BloomFilter.class);
private static RedisTemplate
FastJson2JsonRedisSerializer
@Override
public byte[] serialize(Object o) throws SerializationException {
try {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
Hessian2Output hessian2Output = new Hessian2Output(byteArrayOutputStream);
hessian2Output.setSerializerFactory(serializerFactory);
hessian2Output.startMessage();
hessian2Output.writeObject(o);
hessian2Output.completeMessage();
hessian2Output.close();
return byteArrayOutputStream.toByteArray();
} catch (IOException e) {
log.warn("序列化异常");
throw new RuntimeException(e);
}
}
@Override
public Object deserialize(byte[] bytes) throws SerializationException {
Hessian2Input hessian2Input = new Hessian2Input(new ByteArrayInputStream(bytes));
hessian2Input.setSerializerFactory(serializerFactory);
try {
hessian2Input.startMessage();
Object o = hessian2Input.readObject();
hessian2Input.completeMessage();
hessian2Input.close();
System.out.println("[serial]"+o);
return o;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
});
template.afterPropertiesSet();
return template;
}
/**
* 千万分之的概率命中失败
* 命中:准确率
*/
private static final BloomFilter
private static final long serialVersionUID = 1L;
@Override
public void funnel(String value, PrimitiveSink sink) {
sink.putString(value, Charsets.UTF_8);
}
}, 1024*1024*32, 0.0000001d);
public static void main(String[] args) throws InterruptedException, IOException {
hasNoFeedBackUser.put("zhangwei");
System.out.println(hasNoFeedBackUser.mightContain("zhangwei"));
//发序列化错误
CityDTO cityDTO = new CityDTO();
cityDTO.setCityId(123);
cityDTO.setCityName("济南");
stringRedisTemplate.opsForValue().set("dw",cityDTO,12,TimeUnit.MINUTES);
Thread.sleep(1000L);
Object zw = stringRedisTemplate.opsForValue().get("dw");
CityDTO bloomFilter = (CityDTO) zw;
System.out.println("[应用]"+bloomFilter);
stringRedisTemplate.opsForValue().set("zs",hasNoFeedBackUser,12,TimeUnit.MINUTES);
Thread.sleep(1000L);
Object zw1 = stringRedisTemplate.opsForValue().get("zs");
BloomFilter bloom = (BloomFilter) zw1;
System.out.println(bloom.mightContain("zhangwei"));
System.out.println(bloom.mightContain("zhangwei11"));
System.out.println("[应用]"+bloom);
}
}