铁匠 铁匠
首页
收藏
java
架构之路
常用算法
  • Java
  • nginx
  • 系统运维
  • 系统安全
  • mysql
  • redis
参考文档
关于
链接
  • 分类
  • 标签
  • 归档

专注、不予评判地关注当下
首页
收藏
java
架构之路
常用算法
  • Java
  • nginx
  • 系统运维
  • 系统安全
  • mysql
  • redis
参考文档
关于
链接
  • 分类
  • 标签
  • 归档
  • 概览

  • 设计模式

    • 软件设计原则
    • 创建型

    • 结构型

    • 行为型

      • 观察者模式
      • 模板方法
      • 策略模式
  • 高可用

  • 性能优化

  • 分布式

  • 网关

  • 流量治理

  • 数据治理

  • 云原生

  • 网络安全

  • 架构
  • 设计模式
  • 行为型
fengjx
2023-01-09

策略模式

定义一组算法,并封装好其中每一个,使它们可以互相替换。策略模式使算法的变化独立于使用它的客户。

一个 redis 中的例子:redis 内存写满之后会通过不同的策略将数据删除,继续提供服务。使用策略模式模拟redis中的4中淘汰策略(random、ttl、lru、lfu)

public interface EvictionStrategy {

    /**
     * 筛选出淘汰的 key
     */
    List<String> selectKeys();
}

public class LfuStrategy implements EvictionStrategy {

    @Override
    public List<String> selectKeys() {
        System.out.println("使用 lfu 淘汰策略");
        return Collections.singletonList("lfu_key");
    }
}

public class LruStrategy implements EvictionStrategy {

    @Override
    public List<String> selectKeys() {
        System.out.println("使用 lru 淘汰策略");
        return Collections.singletonList("lru_key");
    }
}

public class RandomStrategy implements EvictionStrategy {

    @Override
    public List<String> selectKeys() {
        System.out.println("使用 random 淘汰策略");
        return Collections.singletonList("random_key");
    }
}

public class TtlStrategy implements EvictionStrategy {

    @Override
    public List<String> selectKeys() {
        System.out.println("使用 ttl 淘汰策略");
        return Collections.singletonList("ttl_key");
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
public class EvictionStrategyFactory {

    private static final Map<String, EvictionStrategy> STRATEGY_MAP = new HashMap<>();

    static {
        STRATEGY_MAP.put("ttl", new TtlStrategy());
        STRATEGY_MAP.put("lru", new LruStrategy());
        STRATEGY_MAP.put("lfu", new LfuStrategy());
        STRATEGY_MAP.put("random", new RandomStrategy());
    }

    public static EvictionStrategy getStrategy(String type) {
        return STRATEGY_MAP.get(type);
    }

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public class RedisServer {

    private EvictionStrategy strategy;

    public RedisServer(final EvictionStrategy strategy) {
        this.strategy = strategy;
    }

    // 根据策略淘汰缓存
    public void clean() {
        List<String> keys = strategy.selectKeys();
        System.out.println("淘汰缓存: " + keys);
    }

    // 动态修改缓存淘汰策略
    public void changeStrategy(String type) {
        System.out.println("修改缓存策略: " + type);
        strategy = EvictionStrategyFactory.getStrategy(type);
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

测试类

public class StrategyApp {

    public static void main(String[] args) {
        String type = getTypeFromConfig("redis.conf");
        EvictionStrategy strategy = EvictionStrategyFactory.getStrategy(type);
        RedisServer server = new RedisServer(strategy);
        server.clean();

        server.changeStrategy("lfu");
        server.clean();

    }

    private static String getTypeFromConfig(String configFilePath) {
        return "lru";
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#策略模式
模板方法
性能测试神器 wrk 使用教程

← 模板方法 性能测试神器 wrk 使用教程→

最近更新
01
模板方法
01-06
02
观察者模式
01-06
03
代理模式
01-03
更多文章>
Theme by Vdoing | Copyright © 2016-2023 铁匠 | 粤ICP备15021633号
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式