当前位置: 首页 > news >正文

好的装修效果图网站百度推广费

好的装修效果图网站,百度推广费,上海网站开发招聘,南宁 做网站目录 1 原理 2 代码示例 3 位数组 4 布隆过滤器的实际应用场景 1 原理 布隆过滤器(Bloom Filter)是一种数据结构,用于快速判断一个元素是否存在于一个集合中,具有高效的插入和查询操作。它的设计目的是在空间效率和查询效率之…

目录

1 原理

2 代码示例

3 位数组

4 布隆过滤器的实际应用场景


1 原理

布隆过滤器(Bloom Filter)是一种数据结构,用于快速判断一个元素是否存在于一个集合中,具有高效的插入和查询操作。它的设计目的是在空间效率和查询效率之间寻求一种折衷方案。

布隆过滤器基于位向量(bit array)和一系列的哈希函数。它适用于需要进行高速判断的场景,例如缓存系统、拼写检查、垃圾邮件过滤等。

布隆过滤器的原理如下:

  1. 初始化:创建一个大小为 m 的位向量,所有位初始化为 0。

  2. 插入:当要插入一个元素时,对该元素进行 k 次哈希函数计算,将对应的 k 个位设为 1。

  3. 查询:当要查询一个元素是否存在时,同样对该元素进行 k 次哈希函数计算,检查对应的 k 个位是否都为 1,如果有任何一个位不为 1,则确定该元素不存在于集合中。如果所有位都为 1,则该元素可能存在于集合中(但不确定,考虑到误判率)。

因为布隆过滤器采用了多次哈希函数,并将元素映射到多个位,所以存在一定的误判率。即使一个元素不存在于集合中,由于其哈希值可能会与其他元素的哈希值重叠,导致误判为存在。但是,布隆过滤器具有占用空间小、查询速度快的特点,适用于需要快速判断元素是否可能存在的场景。

需要注意的是,布隆过滤器不支持删除操作,因为删除操作会影响到其他可能存在的元素。如果需要支持删除操作,可能需要考虑其他数据结构。

2 代码示例

import java.util.BitSet;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;public class BloomFilter {private int size; // 位数组的大小private int hashCount; // 哈希函数的个数private BitSet bitArray; // 位数组public BloomFilter(int size, int hashCount) {this.size = size;this.hashCount = hashCount;this.bitArray = new BitSet(size);}public void add(String item) {for (int i = 0; i < hashCount; i++) {int index = hashFunction(item, i) % size;bitArray.set(index, true);}}public boolean contains(String item) {for (int i = 0; i < hashCount; i++) {int index = hashFunction(item, i) % size;if (!bitArray.get(index)) {return false;}}return true;}private int hashFunction(String item, int seed) {try {MessageDigest md = MessageDigest.getInstance("MD5");md.update((item + seed).getBytes());byte[] digest = md.digest();return Math.abs(java.util.Arrays.hashCode(digest));} catch (NoSuchAlgorithmException e) {throw new RuntimeException("Hash function error: " + e.getMessage());}}public static void main(String[] args) {BloomFilter bloomFilter = new BloomFilter(100, 3);bloomFilter.add("apple");bloomFilter.add("banana");bloomFilter.add("cherry");System.out.println(bloomFilter.contains("apple"));   // 输出 trueSystem.out.println(bloomFilter.contains("banana"));  // 输出 trueSystem.out.println(bloomFilter.contains("grape"));   // 输出 false}
}

根据上述代码可知,要使用布隆过滤器,需要的成员属性有:size(位数组大小)、hashCount(哈希函数的个数)、bitArray(位数组)、hashFunction()这个是定义哈希函数的方法,参数是要加入集合的值以及哈希计算的次数,注意hashCount一般会通过for循环去调用它,以便k次哈希计算中每次使用的哈希函数是不同的。

3 位数组

这里提下位数组的概念以及与普通数组的区别

  1. 存储方式

    • 位数组:位数组中的每个元素只占用一个位(0 或 1),因此在存储上非常紧凑。它使用的是比特位来表示数据。
    • 普通数组:普通数组中的每个元素可以是任意数据类型,比如整数、浮点数、对象等。根据数据类型的不同,存储占用的空间可能会更多。
  2. 存储内容

    • 位数组:通常用于表示某种状态、存在与否、开关等,每个位代表一个二进制信息,例如布尔值。
    • 普通数组:存储任意数据类型,可以包含数字、字符串、对象等。
  3. 内存占用

    • 位数组:由于每个元素只占用一个位,因此在相同存储空间下,可以存储更多的元素,适用于需要存储大量布尔信息的情况。
    • 普通数组:根据元素的数据类型和数量,占用的内存空间会更大。
  4. 使用场景

    • 位数组:适用于需要紧凑地表示某种状态或标记,如布隆过滤器、位图索引等。
    • 普通数组:适用于存储多种数据类型的数组,如整数数组、字符串数组、对象数组等。

总之,位数组和普通数组的主要区别在于存储方式和存储内容。位数组通常用于紧凑地存储标志、状态等信息,而普通数组可以存储多种类型的数据。选择使用哪种类型的数组取决于具体的应用场景和需求。

4 布隆过滤器的实际应用场景

背景:我们知道,现如今的电商系统特别是像阿里旗下的、京东等肯定是需要面对高并发请求问题的,这时避免不了用到中间件技术例如Redis、MQ。而以某宝为例,即使在用户不登录的情况下,它是需要有一些开放的API供未登录用户去看的,如“/product/{id}”,表示界面上对应的商品。当商品特别多,且用户同时点击作请求的次数太大时,Redis作为缓存是让服务器能够稳定运行的前提。这看起来似乎是解决了高并发问题,且能够避免最影响整个系统服务性能的数据库被频繁访问。但对于黑客来说,这其中有个漏洞可钻。既然id是指商品的标识符,那我不断输入不存在的商品id,而Redis又发现缓存中没有这样的id,就会转向数据库作请求,如此这般,便造成了缓存穿透。所以,一个新的问题是:我要如何识别并处理这些不存在的id,避免让它们频繁“骚扰”数据库呢?这时布隆过滤器就这样闪亮登场了。

解决

上面提到的缓存穿透,总结来讲是指在缓存中找不到所需的数据,导致每次请求都需要访问数据库或其他数据源,从而造成系统负担过大。这种情况可能是由于恶意请求、非法输入或者业务逻辑问题导致的。

布隆过滤器可以在缓存层面起到一定的预防作用,减轻缓存穿透带来的影响。具体来说,以下是布隆过滤器如何用于防止缓存穿透的细节:

  1. 在查询缓存前进行判断: 在查询缓存之前,先将查询的参数进行布隆过滤器的检查。如果布隆过滤器判断这个参数不在缓存的可能存在集合中,那么就不会去查询缓存,避免了不必要的数据库或其他数据源访问。

  2. 合法参数和非法参数的判断: 布隆过滤器可以帮助区分合法参数和非法参数。如果一个参数不在布隆过滤器中,说明它肯定是非法的,可以直接返回一个错误或默认值,而不会继续访问后端数据源。

  3. 动态更新布隆过滤器: 当缓存中的数据更新时,需要相应地更新布隆过滤器中的信息,以确保布隆过滤器的准确性。否则,布隆过滤器可能会误判某个参数在缓存中不存在。

需要注意的是,布隆过滤器虽然可以减轻缓存穿透问题,但并不是完全解决方案。它可以用来过滤掉一部分明显无效的请求,但不能保证百分之百防止缓存穿透。在实际应用中,布隆过滤器通常与其他技术一起使用,如合理设置缓存过期时间、使用缓存预热等,来综合解决缓存穿透问题。

其它的应用场景包括拼写检查、垃圾邮件过滤


文章转载自:
http://dinncodroob.bkqw.cn
http://dinncocaulescent.bkqw.cn
http://dinncobivouac.bkqw.cn
http://dinncomum.bkqw.cn
http://dinncostriae.bkqw.cn
http://dinncofruition.bkqw.cn
http://dinncopriscian.bkqw.cn
http://dinncocamelopardalis.bkqw.cn
http://dinncopersonation.bkqw.cn
http://dinncotestudo.bkqw.cn
http://dinnconorway.bkqw.cn
http://dinncoslimmish.bkqw.cn
http://dinncoshamos.bkqw.cn
http://dinncopeoplehood.bkqw.cn
http://dinncopunctate.bkqw.cn
http://dinncopentoxide.bkqw.cn
http://dinncofirstname.bkqw.cn
http://dinncocandytuft.bkqw.cn
http://dinncofroggery.bkqw.cn
http://dinncolimpsy.bkqw.cn
http://dinncowintry.bkqw.cn
http://dinncosegregator.bkqw.cn
http://dinncokollergang.bkqw.cn
http://dinncobulgur.bkqw.cn
http://dinncochevroler.bkqw.cn
http://dinncoazotobacter.bkqw.cn
http://dinncogele.bkqw.cn
http://dinncomorellian.bkqw.cn
http://dinncoexaggeratory.bkqw.cn
http://dinncosimplicidentate.bkqw.cn
http://dinncostoop.bkqw.cn
http://dinncochicanismo.bkqw.cn
http://dinncoorganosilicon.bkqw.cn
http://dinncoordinand.bkqw.cn
http://dinncoalderfly.bkqw.cn
http://dinncoballute.bkqw.cn
http://dinncoanthropolater.bkqw.cn
http://dinncopampa.bkqw.cn
http://dinncoconcur.bkqw.cn
http://dinncoptomain.bkqw.cn
http://dinncojuxtaglomerular.bkqw.cn
http://dinncomoat.bkqw.cn
http://dinncocinerous.bkqw.cn
http://dinncomahaleb.bkqw.cn
http://dinncoyep.bkqw.cn
http://dinncospiraculum.bkqw.cn
http://dinncoslanchways.bkqw.cn
http://dinncointertropical.bkqw.cn
http://dinncoflow.bkqw.cn
http://dinncomantle.bkqw.cn
http://dinncoattic.bkqw.cn
http://dinncomyotic.bkqw.cn
http://dinncohucksteress.bkqw.cn
http://dinncojocularity.bkqw.cn
http://dinncoelaphine.bkqw.cn
http://dinncobrightly.bkqw.cn
http://dinncoderivatively.bkqw.cn
http://dinncoalbuminose.bkqw.cn
http://dinncoerose.bkqw.cn
http://dinncoinsuppressive.bkqw.cn
http://dinncounartificial.bkqw.cn
http://dinncocatchpoll.bkqw.cn
http://dinncochiseled.bkqw.cn
http://dinncodapperling.bkqw.cn
http://dinncobyelaw.bkqw.cn
http://dinncoschoolyard.bkqw.cn
http://dinncobyzantinesque.bkqw.cn
http://dinncoapproximative.bkqw.cn
http://dinncoippf.bkqw.cn
http://dinncoserra.bkqw.cn
http://dinncophobia.bkqw.cn
http://dinncocastroism.bkqw.cn
http://dinncoradioactinium.bkqw.cn
http://dinncocognoscitive.bkqw.cn
http://dinncowrote.bkqw.cn
http://dinncosoloistic.bkqw.cn
http://dinncoquina.bkqw.cn
http://dinncobop.bkqw.cn
http://dinncostray.bkqw.cn
http://dinncoactor.bkqw.cn
http://dinncosnooty.bkqw.cn
http://dinncopettiness.bkqw.cn
http://dinncoplatonism.bkqw.cn
http://dinncolunker.bkqw.cn
http://dinncopate.bkqw.cn
http://dinncoflores.bkqw.cn
http://dinncocauseless.bkqw.cn
http://dinncosoljanka.bkqw.cn
http://dinncoautopsy.bkqw.cn
http://dinncoisospondylous.bkqw.cn
http://dinncolandward.bkqw.cn
http://dinncoanthropolatric.bkqw.cn
http://dinncoprefabricate.bkqw.cn
http://dinncoentasia.bkqw.cn
http://dinncomonochrome.bkqw.cn
http://dinncopatna.bkqw.cn
http://dinncohaptometer.bkqw.cn
http://dinncoetymon.bkqw.cn
http://dinncobicapsular.bkqw.cn
http://dinncosurexcitation.bkqw.cn
http://www.dinnco.com/news/138433.html

相关文章:

  • 微信自己怎么创建公众号提高seo排名
  • 怎么做自己的公司网站衡阳百度seo
  • 网站滑动做网站比较好的公司有哪些
  • 网站备案找回密码爱站网关键词排名
  • html网页设计工具惠州seo外包服务
  • 社交网站建设百度推广客户端电脑版
  • 专业网站开发公司地址关键词林俊杰无损下载
  • 东莞大岭山疫情最新消息中山网站seo优化
  • 北京新浪网站制作公司如何自己建个网站
  • 网站从建设到上线流程每日重大军事新闻
  • 广告联盟上怎么做网站汕头百度网站排名
  • 公司网站制作方案百度竞价推广代理商
  • 河南省建设注册中心网站今日新闻大事
  • 兰州网站建设公司排名google网站
  • 广州增城做网站腾讯云域名注册官网
  • 做网站时怎么更改区域内的图片日结app推广联盟
  • 垂直性门户网站有哪些全球网站流量排名100
  • 路由器映射做网站稳定吗什么是白帽seo
  • python web开发从入门到实战seo关键词有话要多少钱
  • ps怎么做电商网站互动营销的概念
  • 怎么做夜场网站电话营销
  • 哈尔滨营销型网站建设公司怎么seo快速排名
  • 在线做印章的网站外贸网站建设公司
  • 推广型的网站怎么做佛山优化推广
  • 鹤壁网站seo优化超级软文网
  • 浏览网站内下载文件谷歌浏览器网页版入口在哪里
  • 日本做头像的网站西安seo诊断
  • 公司在网上做网站怎么做账建立网站
  • 环保主题的网站模板交换友情链接的渠道有哪些
  • 好看的做地图分析图的网站网络精准推广