Skip to content

feat(adapter): add lazyLoad parameter to initWithAdapter in newEnforcerWithClass#503

Merged
hsluoyz merged 1 commit intoapache:masterfrom
pswamis:fix/lazy-loading-newEnforcer
Sep 22, 2025
Merged

feat(adapter): add lazyLoad parameter to initWithAdapter in newEnforcerWithClass#503
hsluoyz merged 1 commit intoapache:masterfrom
pswamis:fix/lazy-loading-newEnforcer

Conversation

@pswamis
Copy link
Copy Markdown
Contributor

@pswamis pswamis commented Sep 21, 2025

The newEnforcer function is a key entry point for initializing an Enforcer instance in the Casbin library. This change introduces the ability to control lazy loading during the initialization process. Lazy loading allows users to defer the loading of policies until explicitly required, which can improve performance in scenarios where policy loading is not immediately necessary.

By adding a lazyLoad parameter to the newEnforcer initialization process, users gain more flexibility in managing their application's performance and resource usage. This is particularly useful in environments where policy data is large or where initialization speed is critical.

Before the Change

Previously, the newEnforcer function would always load policies during initialization, regardless of whether they were needed immediately. For example:

const e = await newEnforcer('path/to/model.conf', 'path/to/policy.csv');

This behavior could lead to unnecessary overhead in cases where the policies were not used right away.

After the Change

With the lazyLoad parameter, users can now control when policies are loaded:

// Initialize the enforcer with lazy loading enabled.
const e = await newEnforcer('path/to/model.conf', 'path/to/policy.csv', true);

// Policies are not loaded during initialization. They can be loaded explicitly later:
await e.loadPolicy();

This approach provides the following benefits:

Improved Performance: Policies are loaded only when needed, reducing initialization time.
Resource Optimization: Avoids loading large policy files unnecessarily.
Flexibility: Users can decide the best time to load policies based on their application's workflow.

Scenario: Frequent Dynamic Policy Creation with Large Policy Sets
In some applications, policies are not static but are dynamically created and updated frequently. For example, consider a multi-tenant system where each tenant has millions of policies that are stored in a database. Loading all these policies into memory at once can lead to significant performance bottlenecks, especially when the policies are not immediately required.

With the introduction of the lazyLoad parameter, users can now defer the loading of these large policy sets until they are explicitly needed. This is particularly useful in scenarios where:

Dynamic Policy Creation: Policies are being created or updated frequently, and loading them into memory every time would be inefficient.
Large Policy Sets: The number of policies is in the millions, making in-memory storage impractical due to memory constraints.
Selective Policy Usage: Only a subset of policies is required at any given time, and loading all policies upfront would waste resources.

@hsluoyz hsluoyz merged commit b9a3d45 into apache:master Sep 22, 2025
9 checks passed
github-actions bot pushed a commit that referenced this pull request Sep 22, 2025
# [5.39.0](v5.38.0...v5.39.0) (2025-09-22)

### Features

* **adapter:** add lazyLoad parameter to initWithAdapter in newEnforcerWithClass ([#503](#503)) ([b9a3d45](b9a3d45))
@github-actions
Copy link
Copy Markdown

🎉 This PR is included in version 5.39.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants