Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
S
searchhotcrawler
Overview
Overview
Details
Activity
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
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
zhiwei
searchhotcrawler
Commits
ac0a1aa2
You need to sign in or sign up before continuing.
Commit
ac0a1aa2
authored
Apr 25, 2018
by
zhiwei
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
添加缓存机制
parent
3f8e90d7
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
184 additions
and
0 deletions
+184
-0
src/main/java/com/zhiwei/searchhotcrawler/cache/CacheListener.java
+28
-0
src/main/java/com/zhiwei/searchhotcrawler/cache/CacheManager.java
+113
-0
src/main/java/com/zhiwei/searchhotcrawler/cache/EntityCache.java
+43
-0
No files found.
src/main/java/com/zhiwei/searchhotcrawler/cache/CacheListener.java
0 → 100644
View file @
ac0a1aa2
package
com
.
zhiwei
.
searchhotcrawler
.
cache
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
com.zhiwei.zhiweiTools.tools.ZhiWeiTools
;
public
class
CacheListener
{
Logger
logger
=
LoggerFactory
.
getLogger
(
CacheListener
.
class
);
public
void
startListen
()
{
new
Thread
(){
public
void
run
()
{
while
(
true
)
{
if
(
CacheManager
.
caches
!=
null
&&
CacheManager
.
caches
.
size
()>
0
){
for
(
String
key
:
CacheManager
.
getAllKeys
())
{
if
(
CacheManager
.
isTimeOut
(
key
))
{
CacheManager
.
clearByKey
(
key
);
logger
.
info
(
key
+
"缓存被清除"
);
}
}
}
ZhiWeiTools
.
sleep
(
500
);
}
}
}.
start
();
}
}
src/main/java/com/zhiwei/searchhotcrawler/cache/CacheManager.java
0 → 100644
View file @
ac0a1aa2
package
com
.
zhiwei
.
searchhotcrawler
.
cache
;
import
java.util.Map
;
import
java.util.Set
;
import
java.util.concurrent.ConcurrentHashMap
;
public
class
CacheManager
{
public
static
Map
<
String
,
EntityCache
>
caches
=
new
ConcurrentHashMap
<
String
,
EntityCache
>();
/**
* 存入缓存
* @param key
* @param cache
*/
public
static
void
putCache
(
String
key
,
EntityCache
cache
)
{
caches
.
put
(
key
,
cache
);
}
/**
* 存入缓存
* @param key
* @param cache
*/
public
static
void
putCache
(
String
key
,
Object
datas
,
long
timeOut
)
{
timeOut
=
timeOut
>
0
?
timeOut
:
0L
;
putCache
(
key
,
new
EntityCache
(
datas
,
timeOut
,
System
.
currentTimeMillis
()));
}
/**
* 获取对应缓存
* @param key
* @return
*/
public
static
EntityCache
getCacheByKey
(
String
key
)
{
if
(
isContains
(
key
))
{
return
caches
.
get
(
key
);
}
return
null
;
}
/**
* 获取对应缓存
* @param key
* @return
*/
public
static
Object
getCacheDataByKey
(
String
key
)
{
if
(
isContains
(
key
))
{
return
caches
.
get
(
key
).
getDatas
();
}
return
null
;
}
/**
* 获取所有缓存
* @param key
* @return
*/
public
static
Map
<
String
,
EntityCache
>
getCacheAll
()
{
return
caches
;
}
/**
* 判断是否在缓存中
* @param key
* @return
*/
public
static
boolean
isContains
(
String
key
)
{
return
caches
.
containsKey
(
key
);
}
/**
* 清除所有缓存
*/
public
static
void
clearAll
()
{
caches
.
clear
();
}
/**
* 清除对应缓存
* @param key
*/
public
static
void
clearByKey
(
String
key
)
{
if
(
isContains
(
key
))
{
caches
.
remove
(
key
);
}
}
/**
* 缓存是否超时失效
* @param key
* @return
*/
public
static
boolean
isTimeOut
(
String
key
)
{
if
(!
caches
.
containsKey
(
key
))
{
return
true
;
}
EntityCache
cache
=
caches
.
get
(
key
);
long
timeOut
=
cache
.
getTimeOut
();
long
lastRefreshTime
=
cache
.
getLastRefeshTime
();
if
(
timeOut
==
0
||
System
.
currentTimeMillis
()
-
lastRefreshTime
>=
timeOut
)
{
return
true
;
}
return
false
;
}
/**
* 获取所有key
* @return
*/
public
static
Set
<
String
>
getAllKeys
()
{
return
caches
.
keySet
();
}
}
src/main/java/com/zhiwei/searchhotcrawler/cache/EntityCache.java
0 → 100644
View file @
ac0a1aa2
package
com
.
zhiwei
.
searchhotcrawler
.
cache
;
public
class
EntityCache
{
/**
* 保存的数据
*/
private
Object
datas
;
/**
* 设置数据失效时间,为0表示永不失效
*/
private
long
timeOut
;
/**
* 最后刷新时间
*/
private
long
lastRefeshTime
;
public
EntityCache
(
Object
datas
,
long
timeOut
,
long
lastRefeshTime
)
{
this
.
datas
=
datas
;
this
.
timeOut
=
timeOut
;
this
.
lastRefeshTime
=
lastRefeshTime
;
}
public
Object
getDatas
()
{
return
datas
;
}
public
void
setDatas
(
Object
datas
)
{
this
.
datas
=
datas
;
}
public
long
getTimeOut
()
{
return
timeOut
;
}
public
void
setTimeOut
(
long
timeOut
)
{
this
.
timeOut
=
timeOut
;
}
public
long
getLastRefeshTime
()
{
return
lastRefeshTime
;
}
public
void
setLastRefeshTime
(
long
lastRefeshTime
)
{
this
.
lastRefeshTime
=
lastRefeshTime
;
}
}
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