Skip to content

[Bug]: apply_settings() hits the database on every call #5445

Description

@RITVIKKAMASETTY

Description

apply_settings() hits the database on every call

File: esp/esp/program/modules/handlers/creditcardmodule_stripe.py

Problem

apply_settings() performs a database query and JSON parse on every invocation. Since get_setting() delegates directly to apply_settings(), any code path that calls get_setting() multiple times (e.g. payonline, charge_payment) triggers redundant DB hits within a single request.

def get_setting(self, name, default=None):
    return self.apply_settings().get(name, default)  # re-runs every time

Suggested Fix

Memoize the result on the instance so the DB is only queried once:

def apply_settings(self):
    if hasattr(self, '_settings_cache'):
        return self._settings_cache
    DEFAULTS = {
        'offer_donation': True,
        'donation_text': 'Donation to Learning Unlimited',
        'donation_options': [10, 20, 50],
        'invoice_prefix': settings.INSTITUTION_NAME.lower(),
    }
    DEFAULTS.update(settings.STRIPE_CONFIG)
    tag_data = json.loads(Tag.getProgramTag('stripe_settings', self.program))
    self._settings_cache = {**DEFAULTS, **tag_data}
    return self._settings_cache

Unnecessary repeated DB queries and JSON parsing on every page load of the payment flow.

Steps to Reproduce

No response

Expected Behavior

Settings for a given module instance should be loaded once per request and reused.

Actual Behavior

No response

Screenshots

No response

Operating System

No response

Browser

No response

Additional Context

No response

Metadata

Metadata

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions