### Install Speedgoose Source: https://github.com/arqo123/speedgoose/blob/master/README.md Install Speedgoose using npm or yarn. Note the Mongoose version compatibility. ```bash $ npm install speedgoose # or $ yarn add speedgoose ``` -------------------------------- ### Run Benchmark Tests Source: https://github.com/arqo123/speedgoose/blob/master/README.md Execute the benchmark tests to evaluate Speedgoose performance. Ensure you have yarn installed. ```console yarn benchmark ``` -------------------------------- ### Cache Pipeline with Manual Multitenant Value Source: https://github.com/arqo123/speedgoose/blob/master/README.md When using `.cachePipeline()` in a multitenant setup and the tenant value is not in the query, pass it manually using the `multitenantValue` option. ```typescript const result = await model.aggregate([]).cachePipeline({ multitenantValue: 'someTenantUniqueValue' }); ``` -------------------------------- ### applySpeedGooseCacheLayer Source: https://github.com/arqo123/speedgoose/blob/master/README.md Initializes the SpeedGoose cache layer with provided configuration options. ```APIDOC ## applySpeedGooseCacheLayer(mongoose, speedgooseConfig) ### Description Applies the SpeedGoose cache layer to your Mongoose instance with a given configuration. ### Parameters #### speedgooseConfig - **redisUri** (string) - Optional - Connection string for Redis. - **redisOptions** (RedisOptions) - Optional - Connection options for Redis. - **redisClient** (Redis) - Optional - Pre-built ioredis client. - **multitenancyConfig** ({ multitenantKey: string }) - Optional - Configuration for multitenancy. - **defaultTtl** (number) - Optional - Default TTL value in seconds. - **refreshTtlOnRead** (boolean) - Optional - If true, refreshes TTL on every read. - **debugConfig** ({ enabled?: boolean, debugModels?: string[], debugOperations?: SpeedGooseDebuggerOperations[] }) - Optional - Configuration for debugging mode. - **sharedCacheStrategy** (SharedCacheStrategies) - Optional - Cache strategy for shared results (REDIS or IN_MEMORY). - **enabled** (boolean) - Optional - Indicates if caching is enabled or disabled. - **clearModelCacheOnUpdate** (boolean) - Optional - If true, clears model-level cache on update events. ``` -------------------------------- ### Apply SpeedGoose Cache Layer with Multitenancy Source: https://github.com/arqo123/speedgoose/blob/master/README.md Configure SpeedGoose with multitenancy support by providing a `multitenantKey` in the `multitenancyConfig`. This requires the Redis URI to be set. ```typescript applySpeedGooseCacheLayer(mongoose, { redisUri: process.env.REDIS_URI, multitenancyConfig: { multitenantKey: 'tenantId', }, }); ``` -------------------------------- ### cacheQuery and cachePipeline Source: https://github.com/arqo123/speedgoose/blob/master/README.md Options for caching query and pipeline operations. ```APIDOC ## cacheQuery(operationParams) and cachePipeline(operationParams) ### Description These methods allow you to cache the results of Mongoose queries or aggregation pipelines. ### Parameters #### operationParams - **TTL** (number) - Optional - Duration in seconds for which the query should be cached. Set to 0 to disable caching for this operation. - **multitenantValue** (string) - Optional - Value to distinguish cache keys between tenants when multitenancy is used. - **cacheKey** (string) - Optional - A custom caching key for the operation. - **refreshTtlOnRead** (boolean) - Optional - If true, the TTL will be refreshed when reading from cached results. ``` -------------------------------- ### Cache Query with Manual Multitenant Value Source: https://github.com/arqo123/speedgoose/blob/master/README.md For `.cacheQuery()` in multitenant scenarios where the tenant ID is not part of the query, provide it explicitly using the `multitenantValue` option. ```typescript const result = await model.find({}).cacheQuery({multitenantValue : 'someTenantUniqueValue'}) ``` -------------------------------- ### Configure SpeedGoose Cache Layer Source: https://github.com/arqo123/speedgoose/blob/master/README.md Defines the configuration options for applying the SpeedGoose cache layer, including Redis connection details and cache behavior settings. Required for cache synchronization. ```typescript redisUri?: string; redisOptions?: RedisOptions; redisClient?: Redis; multitenancyConfig?: { multitenantKey: string; }, defaultTtl?: number; refreshTtlOnRead?: boolean; debugConfig?: { enabled?: boolean, debugModels?: string[], debugOperations?: SpeedGooseDebuggerOperations[], }, sharedCacheStrategy?: SharedCacheStrategies, enabled?: boolean, clearModelCacheOnUpdate?: boolean ``` -------------------------------- ### Cache Population with SpeedGoose Source: https://github.com/arqo123/speedgoose/blob/master/README.md Utilize `.cachePopulate()` to cache the results of population operations. Specify the path to be populated. ```typescript const result = await model.find({}).cachePopulate('user') ``` -------------------------------- ### Replace .populate() with .cachePopulate() Source: https://github.com/arqo123/speedgoose/blob/master/README.md To enable caching for population, simply replace the standard Mongoose .populate() method with Speedgoose's .cachePopulate(). This change is minimal and effective. ```typescript // From const result = await MyModel.find({}).populate('user'); // To const result = await MyModel.find({}).cachePopulate('user'); // or const result = await MyModel.find({}).cachePopulate({ path: 'user' }); ``` -------------------------------- ### Cache Query with Tenant ID in Query Source: https://github.com/arqo123/speedgoose/blob/master/README.md For multitenant caching, include the tenant filtering condition directly in the query object when using `.cacheQuery()`. The tenant value will be automatically inferred. ```typescript const result = await model.find({ someCondition : {$gt: 123}, tenantId: "someTenantUniqueValue", ... //rest of the query }).cacheQuery() ``` -------------------------------- ### Cache Multiple Population Paths Source: https://github.com/arqo123/speedgoose/blob/master/README.md You can cache multiple population paths by passing either a space-delimited string of paths or an array of configuration objects to cachePopulate. The array format is preferred for specifying individual options per path. ```typescript // 1) Pass a space-delimited string const result = await MyModel.find({}).cachePopulate("user comments"); // 2) Pass an array of configuration objects // The second approach is the best when you need to specify different options (such as field selection or deep population) for each path. // You can read about it down below. const result = await MyModel.find({}).cachePopulate([ { path: 'user' }, { path: 'comments' } ]); ``` -------------------------------- ### Enable Debug Mode for SpeedGoose Cache Source: https://github.com/arqo123/speedgoose/blob/master/README.md To enable debug mode, pass the multitenantKey into the wrapper configuration. This allows for detailed logging of cache operations. You can optionally specify which Mongoose models and operations to debug. ```typescript applySpeedGooseCacheLayer(mongoose, { redisUri: process.env.REDIS_URI, debugConfig?: { enabled?: true, /** Optional: An array of mongoose models to debug, if not set then the debugger will log operations for all of the models */ debugModels?: ['yourModelName'], /** Optional: An array of operations to debug, if not set then the debugger will log all operations */ debugOperations?: SpeedGooseDebuggerOperations[], } }) ``` -------------------------------- ### Cache Query Operation Parameters Source: https://github.com/arqo123/speedgoose/blob/master/README.md Specifies parameters for caching query operations, including Time-To-Live (TTL), multitenancy values, custom cache keys, and TTL refreshing on read. TTL is in seconds; set to 0 to disable. ```typescript TTL?: number; multitenantValue?: string; cacheKey?: string; refreshTtlOnRead?: boolean; ``` -------------------------------- ### SpeedGoose Cache Auto Cleaner Configuration Source: https://github.com/arqo123/speedgoose/blob/master/README.md Configuration for the SpeedGoose cache auto-cleaner, allowing a callback to determine if a record has been deleted. Useful for soft-delete scenarios. ```typescript wasRecordDeletedCallback?: (record: Document) => boolean ``` -------------------------------- ### Apply Speedgoose Cache Layer with ioredis Client Source: https://github.com/arqo123/speedgoose/blob/master/README.md Pass a pre-configured ioredis client to Speedgoose for advanced connection settings like TLS or Sentinel. This bypasses the redisUri option. ```typescript import Redis from 'ioredis'; import { applySpeedGooseCacheLayer } from 'speedgoose'; import mongoose from 'mongoose'; const myRedisClient = new Redis({ host: 'my-redis.example.com', port: 6380, tls: { rejectUnauthorized: false }, }); applySpeedGooseCacheLayer(mongoose, { redisClient: myRedisClient, }); ``` -------------------------------- ### SpeedGooseCacheAutoCleaner Source: https://github.com/arqo123/speedgoose/blob/master/README.md Configuration for automatically cleaning the cache, particularly for deleted records. ```APIDOC ## SpeedGooseCacheAutoCleaner(options) ### Description Provides a mechanism for automatically cleaning cache entries, especially when records are deleted or marked as deleted. ### Parameters #### options - **wasRecordDeletedCallback** ((record: Document) => boolean) - Optional - A callback function that determines if a record should be considered deleted. It receives the Mongoose document as an argument. ``` -------------------------------- ### Select Specific Fields with cachePopulate Source: https://github.com/arqo123/speedgoose/blob/master/README.md Similar to Mongoose's populate, you can use the 'select' option within cachePopulate to specify which fields of the populated document should be returned. ```typescript const result = await MyModel.find({}).cachePopulate({ path: 'user', select: 'name email' // or { name: 1, email: 1 } }); ``` -------------------------------- ### Cache Mongoose find Query with Lean Source: https://github.com/arqo123/speedgoose/blob/master/README.md Cache results from a `find` query that uses the `.lean()` option. This is effective for performance when you don't need Mongoose documents. ```typescript const result = await model.find({}).lean().cacheQuery() ``` -------------------------------- ### Apply Speedgoose Cache Layer with Redis URI Source: https://github.com/arqo123/speedgoose/blob/master/README.md Integrate Speedgoose into your Mongoose application by wrapping the mongoose instance. Requires a Redis URI for caching. ```typescript import { applySpeedGooseCacheLayer } from 'speedgoose'; import mongoose from 'mongoose'; applySpeedGooseCacheLayer(mongoose, { redisUri: process.env.REDIS_URI, }); ``` -------------------------------- ### Manually Clear Cache for Specific Keys Source: https://github.com/arqo123/speedgoose/blob/master/README.md Provides a method to manually clear the cache for one or more specific cache keys. This operation is asynchronous. ```typescript clearCacheForKeys(cacheKey: string) : Promise ``` -------------------------------- ### clearCacheForKeys Source: https://github.com/arqo123/speedgoose/blob/master/README.md Manually clears the cache for specific cache keys. ```APIDOC ## clearCacheForKeys(cacheKey) ### Description Manually clears the cache entries associated with one or more specified cache keys. ### Parameters #### cacheKey - **key** (string) - The cache key for which to clear the cached results. ``` -------------------------------- ### Configure TTL Inheritance Behavior Source: https://github.com/arqo123/speedgoose/blob/master/README.md The 'ttlInheritance' option determines how the custom TTL interacts with a globally configured TTL. 'fallback' uses the custom TTL only if no global TTL is set, while 'override' ensures the custom TTL always takes precedence. ```typescript const result = await MyModel.find({}).cachePopulate({ path: 'user', ttl: 300, ttlInheritance: TtlInheritance.OVERRIDE }); ``` -------------------------------- ### Check if Query or Pipeline is Cached Source: https://github.com/arqo123/speedgoose/blob/master/README.md Use the `.isCached()` method to determine if a `find` query or an aggregation pipeline has been cached. This can be useful for debugging or conditional logic. ```typescript const isQueryCached = await model.find({}).sort({fieldA : 1}).isCached() ``` ```typescript const isPipelineCached = await model.aggregate([]).isCached() ``` -------------------------------- ### Set Custom TTL for Cached Documents Source: https://github.com/arqo123/speedgoose/blob/master/README.md The 'ttl' option allows you to define a custom Time-To-Live for cached populated documents, specified in seconds. This controls how long the data remains in the cache. ```typescript const result = await MyModel.find({}).cachePopulate({ path: 'user', ttl: 300 // 5 minutes }); ``` -------------------------------- ### Cache Mongoose findOne Query Source: https://github.com/arqo123/speedgoose/blob/master/README.md Use `.cacheQuery()` to cache the results of a `findOne` operation. Ensure the model type is correctly specified. ```typescript const result = await model.findOne({}).cacheQuery() ``` -------------------------------- ### Cache Mongoose Aggregation Pipeline Source: https://github.com/arqo123/speedgoose/blob/master/README.md Cache the results of an aggregation pipeline by calling `.cachePipeline()` on the aggregation object. The pipeline stages should be provided as an array. ```typescript const result = await model.aggregate([]).cachePipeline() ``` -------------------------------- ### Cache Mongoose find Query with Sorting Source: https://github.com/arqo123/speedgoose/blob/master/README.md Cache query results that include sorting. The `.cacheQuery()` method can be chained after `.sort()`. ```typescript const result = await model.find({}).sort({fieldA : 1}).cacheQuery() ``` -------------------------------- ### Enable Auto-Clearing for Schema Source: https://github.com/arqo123/speedgoose/blob/master/README.md Add the SpeedGooseCacheAutoCleaner plugin to your Mongoose schema to automatically clear cache entries when records are modified or deleted. An optional callback can be provided for custom deletion logic. ```typescript import { SpeedGooseCacheAutoCleaner } from 'speedgoose'; Schema.plugin(SpeedGooseCacheAutoCleaner); //additionally you can pass options for example callback for setting the record as deleted Schema.plugin(SpeedGooseCacheAutoCleaner, { wasRecordDeletedCallback }); ``` -------------------------------- ### Manually Clear Cached Results for a Model Source: https://github.com/arqo123/speedgoose/blob/master/README.md Allows manual clearing of all cached results for a given Mongoose model. Optionally, a multitenant value can be provided to clear cache for a specific tenant. ```typescript clearCachedResultsForModel(modelName: string, multitenantValue?: string) : Promise ``` -------------------------------- ### Cache Mongoose count Query Source: https://github.com/arqo123/speedgoose/blob/master/README.md Cache the result of a `count` operation by appending `.cacheQuery()`. This is useful for counting documents that match specific criteria. ```typescript const result = await model.count({age: {$gt : 25}}).cacheQuery() ``` -------------------------------- ### clearCachedResultsForModel Source: https://github.com/arqo123/speedgoose/blob/master/README.md Manually clears all cached results for a given Mongoose model. ```APIDOC ## clearCachedResultsForModel(modelName, tenantId) ### Description Manually clears all cached results for a specified Mongoose model. This is useful for ensuring cache consistency, especially for aggregate queries that don't include document identifiers. ### Parameters #### modelName - **modelName** (string) - The name of the registered Mongoose model whose cache should be cleared. - **multitenantValue** (string) - Optional - A unique value identifying the tenant, used to clear cache specific to that tenant. ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.