forked from modouxiansheng/Doraemon
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
14faab8
commit 6e195a8
Showing
6 changed files
with
115 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
spring-redis/src/main/java/com/example/springredis/SpringRedisApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,23 @@ | ||
package com.example.springredis; | ||
|
||
import com.example.springredis.hotkey.HotKey; | ||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@SpringBootApplication | ||
@RestController | ||
public class SpringRedisApplication { | ||
|
||
public static void main(String[] args) { | ||
SpringApplication.run(SpringRedisApplication.class, args); | ||
} | ||
|
||
|
||
@RequestMapping("/hotkey/{key}") | ||
public String getHotKey(@PathVariable String key){ | ||
return HotKey.getHotValue(key); | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
spring-redis/src/main/java/com/example/springredis/hotkey/HotKey.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package com.example.springredis.hotkey; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.concurrent.CopyOnWriteArrayList; | ||
|
||
/** | ||
* @program: springBootPractice | ||
* @description: | ||
* @author: hu_pf | ||
* @create: 2019-07-09 18:02 | ||
**/ | ||
public class HotKey { | ||
|
||
private static Map<String,String> hotKeyMap = new ConcurrentHashMap<>(); | ||
private static List<String> hotKeyList = new CopyOnWriteArrayList<>(); | ||
|
||
static { | ||
setHotKey("hu1","1"); | ||
setHotKey("hu2","2"); | ||
} | ||
|
||
public static void setHotKey(String key,String value){ | ||
hotKeyMap.put(key,value); | ||
hotKeyList.add(key); | ||
} | ||
|
||
public static void updateHotKey(String key,String value){ | ||
hotKeyMap.put(key,value); | ||
} | ||
|
||
public static String getHotValue(String key){ | ||
return hotKeyMap.get(key); | ||
} | ||
|
||
public static void removeHotKey(String key){ | ||
hotKeyMap.remove(key); | ||
} | ||
|
||
public static boolean containKey(String key){ | ||
return hotKeyList.contains(key); | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
spring-redis/src/main/java/com/example/springredis/hotkey/HotKeyAction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.example.springredis.hotkey; | ||
|
||
|
||
public enum HotKeyAction { | ||
|
||
UPDATE(){ | ||
@Override | ||
public void action(String key, String value) { | ||
HotKey.updateHotKey(key,value); | ||
} | ||
}, | ||
REMOVE(){ | ||
@Override | ||
public void action(String key, String value) { | ||
HotKey.removeHotKey(key); | ||
} | ||
}; | ||
public abstract void action(String key,String value); | ||
} |
28 changes: 28 additions & 0 deletions
28
spring-redis/src/main/java/com/example/springredis/redis/KeyExpiredEventMessageListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,44 @@ | ||
package com.example.springredis.redis; | ||
|
||
import com.example.springredis.hotkey.HotKey; | ||
import com.example.springredis.hotkey.HotKeyAction; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.data.redis.connection.Message; | ||
import org.springframework.data.redis.connection.MessageListener; | ||
import org.springframework.data.redis.core.RedisTemplate; | ||
import org.springframework.stereotype.Component; | ||
|
||
|
||
@Slf4j | ||
@Component | ||
public class KeyExpiredEventMessageListener implements MessageListener { | ||
|
||
@Autowired | ||
private RedisTemplate redisTemplate; | ||
|
||
@Override | ||
public void onMessage(Message message, byte[] pattern) { | ||
String key = new String(message.getChannel()); | ||
key = key.substring(key.indexOf(":")+1); | ||
String action = new String(message.getBody()); | ||
if (HotKey.containKey(key)){ | ||
String value = redisTemplate.opsForValue().get(key)+""; | ||
switch (action){ | ||
case "set": | ||
log.info("热点Key:{} 修改",key); | ||
HotKeyAction.UPDATE.action(key,value); | ||
break; | ||
case "expired": | ||
log.info("热点Key:{} 到期删除",key); | ||
HotKeyAction.REMOVE.action(key,null); | ||
break; | ||
case "del": | ||
log.info("热点Key:{} 删除",key); | ||
HotKeyAction.REMOVE.action(key,null); | ||
break; | ||
} | ||
} | ||
log.info("监听到的信息:{},值是:{}", new String(message.getChannel()), new String(message.getBody())); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters