Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
S
spring-boot-starter-dsp
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
shenjiaqing
spring-boot-starter-dsp
Commits
1833aba5
Commit
1833aba5
authored
Dec 13, 2021
by
王伟
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
1.增加util模块
2.增加布隆过滤器
parent
d2319b31
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
191 additions
and
1 deletion
+191
-1
build.gradle
build.gradle
+1
-1
settings.gradle
settings.gradle
+1
-0
build.gradle
spring-boot-starter-dsp-util/build.gradle
+22
-0
RedisBloomHandler.java
...duiba/spring/boot/starter/dsp/util/RedisBloomHandler.java
+167
-0
No files found.
build.gradle
View file @
1833aba5
...
...
@@ -38,7 +38,7 @@ allprojects {
}
group
=
"cn.com.duiba.boot"
version
=
"0.0.1
8
"
version
=
"0.0.1
9
"
}
subprojects
{
...
...
settings.gradle
View file @
1833aba5
...
...
@@ -2,4 +2,5 @@ rootProject.name = 'spring-boot-starter-dsp'
include
'spring-boot-starter-dsp-model'
include
'spring-boot-starter-dsp-sampler'
include
'spring-boot-starter-dsp-warmup'
include
'spring-boot-starter-dsp-util'
spring-boot-starter-dsp-util/build.gradle
0 → 100644
View file @
1833aba5
plugins
{
id
'java'
}
description
=
"dsp-util"
repositories
{
mavenCentral
()
}
dependencies
{
testImplementation
'org.junit.jupiter:junit-jupiter-api:5.7.0'
testRuntimeOnly
'org.junit.jupiter:junit-jupiter-engine:5.7.0'
compile
"cn.com.duiba.boot:spring-boot-starter-redis"
compile
(
"redis.clients:jedis"
)
}
test
{
useJUnitPlatform
()
}
\ No newline at end of file
spring-boot-starter-dsp-util/src/main/java/cn/com/duiba/spring/boot/starter/dsp/util/RedisBloomHandler.java
0 → 100644
View file @
1833aba5
package
cn
.
com
.
duiba
.
spring
.
boot
.
starter
.
dsp
.
util
;
import
org.springframework.data.redis.core.RedisTemplate
;
import
org.springframework.data.redis.serializer.RedisSerializer
;
import
org.springframework.stereotype.Component
;
import
org.springframework.util.Assert
;
import
javax.annotation.Resource
;
import
java.util.List
;
import
java.util.Objects
;
import
java.util.concurrent.TimeUnit
;
/**
* @author wangwei
* @since 2021/12/7 11:10 上午
*/
@Component
public
class
RedisBloomHandler
<
K
,
V
>
{
private
static
final
String
RESERVE
=
"BF.RESERVE"
;
private
static
final
String
ADD
=
"BF.ADD"
;
private
static
final
String
MADD
=
"BF.MADD"
;
private
static
final
String
EXISTS
=
"BF.EXISTS"
;
private
static
final
String
MEXISTS
=
"BF.MEXISTS"
;
@Resource
(
name
=
"redisBloomRedisTemplate"
)
private
RedisTemplate
redisTemplate
;
RedisSerializer
keySerializer
()
{
return
redisTemplate
.
getKeySerializer
();
}
RedisSerializer
valueSerializer
()
{
return
redisTemplate
.
getValueSerializer
();
}
RedisSerializer
hashKeySerializer
()
{
return
redisTemplate
.
getHashKeySerializer
();
}
RedisSerializer
hashValueSerializer
()
{
return
redisTemplate
.
getHashValueSerializer
();
}
RedisSerializer
stringSerializer
()
{
return
redisTemplate
.
getStringSerializer
();
}
public
void
createFilter
(
K
key
,
double
errorRate
,
long
initCapacity
)
{
byte
[]
rawKey
=
rawKey
(
key
);
byte
[]
rawErrorRate
=
rawString
(
String
.
valueOf
(
errorRate
));
byte
[]
rawInitCapacity
=
rawString
(
String
.
valueOf
(
initCapacity
));
Object
execute
=
redisTemplate
.
execute
(
connection
->
{
connection
.
execute
(
RESERVE
,
rawKey
,
rawErrorRate
,
rawInitCapacity
);
return
null
;
},
true
);
}
public
void
createFilters
(
List
<
K
>
keys
,
double
errorRate
,
long
initCapacity
)
{
byte
[]
rawErrorRate
=
rawString
(
String
.
valueOf
(
errorRate
));
byte
[]
rawInitCapacity
=
rawString
(
String
.
valueOf
(
initCapacity
));
Object
execute
=
redisTemplate
.
execute
(
connection
->
{
keys
.
forEach
(
key
->
{
byte
[]
rawKey
=
rawKey
(
key
);
connection
.
execute
(
RESERVE
,
rawKey
,
rawErrorRate
,
rawInitCapacity
);
});
return
null
;
},
true
);
}
public
void
createFilterWithExpire
(
K
key
,
double
errorRate
,
long
initCapacity
,
long
seconds
)
{
byte
[]
rawKey
=
rawKey
(
key
);
byte
[]
rawErrorRate
=
rawString
(
String
.
valueOf
(
errorRate
));
byte
[]
rawInitCapacity
=
rawString
(
String
.
valueOf
(
initCapacity
));
Object
execute
=
redisTemplate
.
execute
(
connection
->
{
connection
.
execute
(
RESERVE
,
rawKey
,
rawErrorRate
,
rawInitCapacity
);
connection
.
expire
(
rawKey
,
seconds
);
return
null
;
},
true
);
}
public
void
createFiltersWithExpire
(
List
<
K
>
keys
,
double
errorRate
,
long
initCapacity
,
long
seconds
)
{
byte
[]
rawErrorRate
=
rawString
(
String
.
valueOf
(
errorRate
));
byte
[]
rawInitCapacity
=
rawString
(
String
.
valueOf
(
initCapacity
));
Object
execute
=
redisTemplate
.
execute
(
connection
->
{
keys
.
forEach
(
key
->
{
byte
[]
rawKey
=
rawKey
(
key
);
connection
.
execute
(
RESERVE
,
rawKey
,
rawErrorRate
,
rawInitCapacity
);
connection
.
expire
(
rawKey
,
seconds
);
});
return
null
;
},
true
);
}
public
Boolean
add
(
K
key
,
V
value
)
{
byte
[]
rawKey
=
rawKey
(
key
);
byte
[]
rawValue
=
rawValue
(
value
);
return
(
Boolean
)
redisTemplate
.
execute
(
connection
->
{
Long
l
=
(
Long
)
connection
.
execute
(
ADD
,
rawKey
,
rawValue
);
return
Objects
.
equals
(
l
,
1L
);
},
true
);
}
public
Boolean
[]
addMulti
(
K
key
,
V
...
values
)
{
byte
[][]
rawArgs
=
rawArgs
(
key
,
values
);
return
(
Boolean
[])
redisTemplate
.
execute
(
connection
->
{
List
<
Long
>
ls
=
(
List
<
Long
>)
connection
.
execute
(
MADD
,
rawArgs
);
return
ls
.
stream
().
map
(
l
->
Objects
.
equals
(
l
,
1L
)).
toArray
(
Boolean
[]::
new
);
},
true
);
}
public
boolean
exists
(
K
key
,
V
value
)
{
byte
[]
rawKey
=
rawKey
(
key
);
byte
[]
rawValue
=
rawValue
(
value
);
return
(
boolean
)
redisTemplate
.
execute
(
connection
->
{
Long
l
=
(
Long
)
connection
.
execute
(
EXISTS
,
rawKey
,
rawValue
);
return
Objects
.
equals
(
l
,
1L
);
},
true
);
}
public
Boolean
[]
existsMulti
(
K
key
,
V
...
values
)
{
byte
[][]
rawArgs
=
rawArgs
(
key
,
values
);
return
(
Boolean
[])
redisTemplate
.
execute
(
connection
->
{
List
<
Long
>
ls
=
(
List
<
Long
>)
connection
.
execute
(
MEXISTS
,
rawArgs
);
return
ls
.
stream
().
map
(
l
->
Objects
.
equals
(
l
,
1L
)).
toArray
(
Boolean
[]::
new
);
},
true
);
}
public
Boolean
delete
(
K
key
)
{
return
redisTemplate
.
delete
(
key
);
}
public
Boolean
expire
(
K
key
,
long
timeOut
,
TimeUnit
timeUnit
)
{
return
redisTemplate
.
expire
(
key
,
timeOut
,
timeUnit
);
}
public
Boolean
hasBloom
(
K
key
)
{
return
redisTemplate
.
hasKey
(
key
);
}
byte
[]
rawKey
(
Object
key
)
{
Assert
.
notNull
(
key
,
"non null key required"
);
return
this
.
keySerializer
()
==
null
&&
key
instanceof
byte
[]
?
(
byte
[])
((
byte
[])
key
)
:
this
.
keySerializer
().
serialize
(
key
);
}
byte
[]
rawString
(
String
key
)
{
return
this
.
stringSerializer
().
serialize
(
key
);
}
byte
[]
rawValue
(
Object
value
)
{
return
this
.
valueSerializer
()
==
null
&&
value
instanceof
byte
[]
?
(
byte
[])
((
byte
[])
value
)
:
this
.
valueSerializer
().
serialize
(
value
);
}
private
byte
[][]
rawArgs
(
Object
key
,
Object
...
values
)
{
byte
[][]
rawArgs
=
new
byte
[
1
+
values
.
length
][];
int
i
=
0
;
rawArgs
[
i
++]
=
rawKey
(
key
);
for
(
Object
value
:
values
)
{
rawArgs
[
i
++]
=
rawValue
(
value
);
}
return
rawArgs
;
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment