AEM 6.5 集成 Redis 详细步骤(附代码)

一、环境准备

  1. Redis 安装

# Ubuntu/Debian系统
sudo apt update
sudo apt install redis-server

# 启动Redis
sudo systemctl start redis-server

# 验证运行状态
sudo systemctl status redis-server
View Code

   2.配置 Redis 远程访问

# 编辑配置文件
sudo nano /etc/redis/redis.conf

# 找到bind 127.0.0.1 ::1并修改为(生产环境建议设置具体IP)
bind 0.0.0.0

# 启用密码认证(推荐)
requirepass your_redis_password

# 重启Redis
sudo systemctl restart redis-server
View Code

二、AEM 端配置

  1.添加 Redis 客户端依赖

  在 AEM 项目的pom.xml中添加:

<dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
    <version>4.4.3</version> <!-- 最新稳定版 -->
</dependency>

  2.创建 Redis 连接工厂

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

public class RedisConnectionFactory {
    private static JedisPool jedisPool;
    
    static {
        JedisPoolConfig poolConfig = new JedisPoolConfig();
        poolConfig.setMaxTotal(100);
        poolConfig.setMaxIdle(10);
        poolConfig.setMinIdle(5);
        poolConfig.setTestOnBorrow(true);
        poolConfig.setTestOnReturn(true);
        
        // 替换为实际的Redis服务器信息
        jedisPool = new JedisPool(
            poolConfig,
            "redis-server-ip",
            6379,
            5000,
            "your_redis_password"
        );
    }
    
    public static Jedis getConnection() {
        return jedisPool.getResource();
    }
    
    public static void closeConnection(Jedis jedis) {
        if (jedis != null) {
            jedis.close();
        }
    }
}

三、缓存实现示例

  1. 创建 Redis 缓存服务

import org.osgi.service.component.annotations.Component;
import redis.clients.jedis.Jedis;

@Component(service = CacheService.class)
public class RedisCacheServiceImpl implements CacheService {
    
    @Override
    public void put(String key, String value) {
        Jedis jedis = null;
        try {
            jedis = RedisConnectionFactory.getConnection();
            jedis.set(key, value);
        } catch (Exception e) {
            // 记录异常日志
            e.printStackTrace();
        } finally {
            RedisConnectionFactory.closeConnection(jedis);
        }
    }
    
    @Override
    public String get(String key) {
        Jedis jedis = null;
        try {
            jedis = RedisConnectionFactory.getConnection();
            return jedis.get(key);
        } catch (Exception e) {
            // 记录异常日志
            e.printStackTrace();
            return null;
        } finally {
            RedisConnectionFactory.closeConnection(jedis);
        }
    }
    
    @Override
    public void delete(String key) {
        Jedis jedis = null;
        try {
            jedis = RedisConnectionFactory.getConnection();
            jedis.del(key);
        } catch (Exception e) {
            // 记录异常日志
            e.printStackTrace();
        } finally {
            RedisConnectionFactory.closeConnection(jedis);
        }
    }
}

    2.缓存服务接口

public interface CacheService {
    void put(String key, String value);
    String get(String key);
    void delete(String key);
}

四、AEM Dispatcher 配置

dispatcher.any中添加 Redis 缓存规则:
/vanityurl {
  /docroot "/content"
  /handler "/libs/granite/dispatcher/content/vanity"
  /invalidate {
    /0001 { /type "path" /glob "/content/**" }
  }
  /cache {
    /docroot "/content"
    /rules {
      /0001 { /type "allow" /glob "*" }
    }
    /headers {
      /0001 { /type "allow" /name "Content-Type" }
    }
    /invalidate {
      /0001 { /type "path" /glob "/content/**" }
    }
    /redis {
      /host "redis-server-ip"
      /port 6379
      /password "your_redis_password"
      /timeout 5000
    }
  }
}

五、验证集成

  1. Redis CLI 测试

redis-cli -h redis-server-ip -a your_redis_password
ping  # 应返回PONG

    2.AEM 代码测试

public class CacheTest {
    public static void main(String[] args) {
        CacheService cacheService = new RedisCacheServiceImpl();
        
        // 存入缓存
        cacheService.put("testKey", "Hello Redis!");
        
        // 获取缓存
        String value = cacheService.get("testKey");
        System.out.println("缓存值: " + value);
        
        // 删除缓存
        cacheService.delete("testKey");
    }
}

六、生产环境建议

  1. 安全加固
    • 禁用 Redis 默认端口 (6379)
    • 配置防火墙限制访问
    • 启用 TLS 加密通信
    • 定期备份 Redis 数据
  2. 性能优化
    • 配置 Redis 内存策略
    • # 在redis.conf中添加
      maxmemory 256mb
      maxmemory-policy allkeys-lru
    • 使用 Redis 集群提高可用性

    3.监控与告警

    • 集成 Prometheus+Grafana 监控 Redis 性能
    • 设置内存使用率、连接数等告警阈值
  通过以上步骤,你可以成功将 AEM 6.5 与最新版 Redis 集成,实现高性能缓存机制。

 

posted @ 2025-06-17 16:15  名曰大神  阅读(13)  评论(0)    收藏  举报