-
-
Notifications
You must be signed in to change notification settings - Fork 4
Open
Description
The package currently exhibits inefficiencies in all methods that make API requests to fetch data from the Umami server. Specifically, these methods perform an authentication request for every API call, regardless of whether the token is already valid or cached. Additionally, the caching strategy for API responses is not leveraged effectively, leading to redundant requests to the server.
Current Behavior
- Authentication Overhead:
All methods that make API requests invokeself::auth()to fetch a token, even if a valid token is already available or could be reused. - Inefficient Use of Cache:
Cached data is often ignored, and requests to the Umami server are made regardless. For example:- Even when
$forceisfalse, API methods fetch data from the server before ultimately returning the cached data. - This defeats the purpose of caching and introduces unnecessary overhead.
- Even when
- Cache Invalidations:
When$forceistrue, the cache is cleared, and a new request is made, but there’s no validation to determine whether this is necessary.
Proposed Improvements
- Token Caching and Validation:
- Cache the authentication token securely and reuse it for subsequent requests.
- Authenticate only when:
- No valid token is cached.
- The cached token is invalid (e.g., expired or rejected by the server).
- Efficient Cache Usage:
- Before making a request to the server, check for valid cached data.
- Skip the server request if cached data is available and $force is false.
- Decouple Responsibilities:
- Separate token management (authentication, caching, and validation) into its own class or service.
- Ensure that API request methods focus solely on fetching and processing data.
- Reusable Strategy:
- Apply these improvements to all methods that make API requests to ensure consistent and efficient behavior throughout the package.
Expected Benefits
- Reduced Overhead:
- Fewer authentication requests and API calls, resulting in better performance.
- Enhanced Scalability:
- Efficient caching will reduce server load and improve response times for frequent queries.
- Better Maintainability:
- A reusable token management strategy and well-separated concerns will make the code easier to maintain and extend.
Steps to Reproduce
- Use any method that makes an API request to the Umami server (e.g., query() or others).
- Observe that:
- An authentication request is made for each API call.
- Cached data is not used effectively, and a request to the server is made even when $force is false.
Possible Solution
- Implement a caching mechanism for the authentication token:
- Validate the token sparingly, such as periodically or when explicitly required.
- Update all API methods to:
- Check for valid cached data before making a server request.
- Only fetch new data when $force is true or cache is unavailable.
I am happy to contribute to addressing this issue by implementing the proposed improvements. Please let me know if you’d like me to proceed with these changes or if there are any additional guidelines/requirements to follow.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels