Skip to content

Add cache invalidation test suite for TTL and write operation scenarios#5

Closed
Copilot wants to merge 3 commits intomainfrom
copilot/create-test-cache-invalidation-script
Closed

Add cache invalidation test suite for TTL and write operation scenarios#5
Copilot wants to merge 3 commits intomainfrom
copilot/create-test-cache-invalidation-script

Conversation

Copy link

Copilot AI commented Feb 10, 2026

Creates test scripts to verify monSQLize's automatic cache invalidation for TTL expiration and write operations (insertOne/updateOne/deleteOne).

Test Scripts

  • test-cache-invalidation.js - Integration test requiring MongoDB

    • TTL expiration: 2s cache → immediate query (HIT) → 2.5s wait → query (MISS)
    • Write ops: query (cache) → write → query (invalidated, MISS)
    • Tracks cache.stats (hits/misses) to verify behavior
    • Supports MONGODB_URI env var for local/remote instances
  • test-cache-invalidation-mock.js - Unit test, no MongoDB required

    • Tests TTL logic, pattern matching deletion, stats tracking
    • Validates core cache invalidation without database dependency
  • TEST-CACHE-README.md - Usage, expected output, troubleshooting

Example Usage

// Run mock test (no MongoDB)
node test-cache-invalidation-mock.js

// Run integration test
MONGODB_URI="mongodb://localhost:27017" node test-cache-invalidation.js

Implementation Notes

Cache invalidation uses cache.delPattern() with namespace pattern matching (*{iid}:{type}:{db}:{collection}*) to clear all queries for the affected collection. TTL uses lazy expiration via expireAt timestamps checked during get() operations.

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • fastdl.mongodb.org
    • Triggering command: /home/REDACTED/work/_temp/ghcca-node/node/bin/node node ./postinstall.js ../deps/cpu_features/include/internal --64 (dns block)
    • Triggering command: /home/REDACTED/work/_temp/ghcca-node/node/bin/node node test-cache-invalidation.js /home/REDACTED/.cache/node-gyp/24.13.0/deps/uv/include -I /home/REDACTED/.cache/node-gyp/24.13.0/deps/zlib -I /home/REDACTED/.cache/node-gyp/24.13.0/deps/v8/include -I ../../../../../nan --64 -o Release/obj.target/sshcrypto/src/binding.o (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Original prompt

创建一个测试脚本来验证 monSQLize 的查询缓存自动失效功能

需求

创建一个新的测试文件 test-cache-invalidation.js,验证以下功能:

1. TTL 自动过期测试

  • 设置缓存 TTL 为 2 秒
  • 查询数据(缓存 miss)
  • 立即再次查询(缓存 hit)
  • 等待 2.5 秒后再次查询(缓存应该过期,重新从数据库读取)

2. 写操作自动失效测试

insertOne 自动失效

  • 查询并缓存数据
  • 执行 insertOne 操作
  • 再次查询,验证缓存已失效(数据应该包含新插入的记录)

updateOne 自动失效

  • 查询并缓存数据
  • 执行 updateOne 更新某个字段
  • 再次查询,验证缓存已失效(数据应该是更新后的)

deleteOne 自动失效

  • 查询并缓存数据
  • 执行 deleteOne 删除操作
  • 再次查询,验证缓存已失效(数据应该少了一条记录)

3. 实现要求

const MonSQLize = require('./index');

async function testCacheInvalidation() {
    console.log('🚀 开始测试缓存自动失效功能\n');

    // 使用内存数据库
    const msq = new MonSQLize({
        type: 'mongodb',
        databaseName: 'test_cache',
        config: { 
            uri: 'memory',  // 使用内存数据库
            options: {}
        },
        cache: {
            maxSize: 1000,
            enableStats: true
        }
    });

    try {
        await msq.connect();
        const collection = msq.dbInstance.collection('test_users');

        // 清理数据
        await collection.deleteMany({});

        // 插入初始数据
        await collection.insertOne({ name: 'Alice', age: 25 });
        await collection.insertOne({ name: 'Bob', age: 30 });

        console.log('=== 测试 1: TTL 自动过期 ===');
        // TODO: 实现 TTL 测试

        console.log('\n=== 测试 2: insertOne 自动失效 ===');
        // TODO: 实现 insertOne 测试

        console.log('\n=== 测试 3: updateOne 自动失效 ===');
        // TODO: 实现 updateOne 测试

        console.log('\n=== 测试 4: deleteOne 自动失效 ===');
        // TODO: 实现 deleteOne 测试

        console.log('\n✅ 所有测试通过!');
    } catch (error) {
        console.error('❌ 测试失败:', error);
        process.exit(1);
    } finally {
        await msq.close();
    }
}

testCacheInvalidation();

4. 验证点

每个测试需要:

  1. 打印缓存统计信息(hits/misses)
  2. 验证数据正确性
  3. 明确标注是否从缓存读取还是从数据库读取

5. 输出格式

🚀 开始测试缓存自动失效功能

=== 测试 1: TTL 自动过期 ===
第一次查询: 2 条记录 (缓存 MISS)
第二次查询: 2 条记录 (缓存 HIT)
等待 2.5 秒...
第三次查询: 2 条记录 (缓存 MISS - TTL 过期)
✓ TTL 自动过期测试通过

=== 测试 2: insertOne 自动失效 ===
查询前: 2 条记录 (缓存)
插入新记录: Charlie
查询后: 3 条记录 (缓存已自动失效)
✓ insertOne 自动失效测试通过

=== 测试 3: updateOne 自动失效 ===
更新前: Alice 的 age = 25
更新 Alice 的 age 为 26
更新后: Alice 的 age = 26 (缓存已自动失效)
✓ updateOne 自动失效测试通过

=== 测试 4: deleteOne 自动失效 ===
删除前: 3 条记录
删除 Charlie
删除后: 2 条记录 (缓存已自动失效)
✓ deleteOne 自动失效测试通过

✅ 所有测试通过!

请实现完整的测试脚本,可以直接运行 node test-cache-invalidation.js 验证。

This pull request was created from Copilot chat.


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI and others added 2 commits February 10, 2026 08:04
Co-authored-by: rockyshi1993 <175696301+rockyshi1993@users.noreply.github.com>
Co-authored-by: rockyshi1993 <175696301+rockyshi1993@users.noreply.github.com>
Copilot AI changed the title [WIP] Add test script for MonSQLize query cache invalidation Add cache invalidation test suite for TTL and write operation scenarios Feb 10, 2026
Copilot AI requested a review from rockyshi1993 February 10, 2026 08:07
Copilot stopped work on behalf of rockyshi1993 due to an error February 10, 2026 08:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants