guava与redisson的BloomFilter

文章目录

  • 1.引入依赖
  • 2.返回客户端实例
  • 3.编写接口类service(包含guava和redisson的接口)
  • 4.实现接口类
  • 5.编写controller

1.引入依赖



<dependency>
    <groupId>redis.clientsgroupId>
    <artifactId>jedisartifactId>
    <version>2.9.0version>
dependency>


<dependency>
    <groupId>com.google.guavagroupId>
    <artifactId>guavaartifactId>
    <version>18.0version>
dependency>


<dependency>
    <groupId>org.redissongroupId>
    <artifactId>redissonartifactId>
    <version>3.8.2version>
dependency>

2.返回客户端实例

package com.example.demo.bloomfilter;

import com.google.common.hash.BloomFilter;
import com.google.common.hash.Funnels;
import org.redisson.Redisson;
import org.redisson.api.RBloomFilter;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.nio.charset.Charset;

@Configuration

public class DataConfig<T> {

    /**
     * 返回guava工具类BloomFilter
     * @return
     */
    @Bean
    public BloomFilter<String> initBloomFilter() {
        return BloomFilter.create(Funnels.stringFunnel(Charset.defaultCharset()), 100);

    }

    /**
     * 返回redisson工具BloomFilter
     * @return
     */

    @Bean
    public RBloomFilter initRedissonClient() {
        Config config = new Config();
        config.useSingleServer().setAddress("redis://ip:port").setDatabase(15).setPassword("123456")
        ;
        RedissonClient redissonClient = Redisson.create(config);
        RBloomFilter<T> book = redissonClient.getBloomFilter("book");
        //容量 容错率
        book.tryInit(10000L, 0.01);
        return book;
    }
}

3.编写接口类service(包含guava和redisson的接口)

package com.example.demo.bloomfilter.service;

public interface BloomFilterService<T> {
    /**
     * guava bloomFilter add
     * @param s
     */
    void add(T s);
    /**
     * guava bloomFilter contain
     * @param s
     * @return
     */
    boolean contain(T s);
    /**
     * Redisson bloomFilter add
     * @param s
     */
    void redissonAdd(T s);
    /**
     * Redisson bloomFilter contain
     * @param s
     * @return
     */

    boolean redissonContain(T s);
}

4.实现接口类

package com.example.demo.bloomfilter.service.impl;

import com.example.demo.bloomfilter.service.BloomFilterService;
import com.google.common.hash.BloomFilter;
import org.redisson.api.RBloomFilter;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class BloomFilterServiceImpl<T> implements BloomFilterService<T> {
    @Resource
    private BloomFilter<T> guavaBloomFilter;
    @Resource
    private RBloomFilter redissonBloomFilter;

    public void add(T s) {
        guavaBloomFilter.put(s);
    }

    public boolean contain(T s) {
        return guavaBloomFilter.mightContain(s);
    }

    @Override
	public void redissonAdd(T s) {

        redissonBloomFilter.add(s);
    }

    @Override
    public boolean redissonContain(T s) {
        return redissonBloomFilter.contains(s);
    }

}

5.编写controller

package com.example.demo.bloomfilter.controller;

import com.example.demo.bloomfilter.service.BloomFilterService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("/bloom")

public class BloomFilterController {
    @Resource
    private BloomFilterService bloomFilterService;

    @RequestMapping("/add")
    public String add() {
        int count =0;
        for (int i = 0; i < 10000; i++) {
            boolean contain = bloomFilterService.contain("book:" + i);
            if (!contain) {
                bloomFilterService.add("book:" + i);
                System.out.println("值__book:" + i + "不存在");
                count++;
            }
        }

        return count+"";
    }
    @RequestMapping("/redissonAdd")
    public String redissonAdd() {
        int count =0;
        for (int i = 0; i < 10000; i++) {
            boolean contain = bloomFilterService.redissonContain("book:" + i);
            if (!contain) {
                bloomFilterService.redissonAdd("book:" + i);
                System.out.println("值__book:" + i + "不存在");
                count++;
            }
        }

        return count+"";
    }
    @RequestMapping("/get")

    public String contain(@RequestParam("value") String value) {
        if (bloomFilterService.contain(value)) {
            System.out.println("查找的值存在");
            return value + "存在";
        } else {
            System.out.println("查找的值不存在");
            return value + "不存在";
        }
    }

    @RequestMapping("/query")
    public String query() {
        int count = 0;
        for (int i = 0; i < 100; i++) {
            boolean contain = bloomFilterService.contain("book:" + i);
            if (!contain) {
                System.out.println("值__" + "book:" + i + "不存在");
                count++;
            }

        }
        System.out.println("统计不存在的值:" + count);
        return count + "";
    }
    @RequestMapping("/redissonQuery")
    public String redissonQuery() {
        int count = 0;
        for (int i = 0; i < 100; i++) {
            boolean contain = bloomFilterService.contain("book:" + i);
            if (!contain) {
                System.out.println("值__" + "book:" + i + "不存在");
                count++;
            }

        }
        System.out.println("统计不存在的值:" + count);
        return count + "";
    }

}

你可能感兴趣的:(guava与redisson的BloomFilter)