Skip to content

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

Closed
localhost-anon wants to merge 2 commits into
apache:masterfrom
localhost-anon:master
Closed

feat(adapter): add lazyLoad parameter to initWithAdapter in newEnforcerWithClass#502
localhost-anon wants to merge 2 commits into
apache:masterfrom
localhost-anon:master

Conversation

@localhost-anon

Copy link
Copy Markdown
Contributor

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.

@CLAassistant

CLAassistant commented Sep 21, 2025

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@localhost-anon localhost-anon changed the title feat: add lazyLoad parameter to initWithAdapter in newEnforcerWithClass feat(adapter): add lazyLoad parameter to initWithAdapter in newEnforcerWithClass Sep 21, 2025
@hsluoyz

hsluoyz commented Sep 21, 2025

Copy link
Copy Markdown
Member

@pswamis why close?

@localhost-anon

Copy link
Copy Markdown
Contributor Author

Hi @hsluoyz , I closed this due to one failing checks. I have created another where all checks are passed.
#503

@hsluoyz

hsluoyz commented Sep 22, 2025

Copy link
Copy Markdown
Member

Replaced by: #503

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.

3 participants