Every successful query results in a collection, and so it is worth reading Collections in Depth before this page.
The basic signature of the Query method is:
The first time you call the Query method with a model type, collection name and criteria, the query is executed on the origin and then the resulting ids are stored in all cache layers using the collection name.
On future calls, the collection name is used to retrieve any past results that match the model type, collection name and criteria. If freshness requirements are met, the cached value is returned.
When you call the Query method, Cascade creates or uses an existing collection to store the results. The collection name is a combination of the provided collectionName parameter and a hash of the criteria object. This approach ensures that different query criteria result in distinct collections, even if they share the same base collection name.
For example:
Internally, Cascade might generate a collection name like "ProductList_hash(criteria)", where the hash is derived from the criteria object.
The criteria object is serialized and becomes part of the collection name. This is crucial for distinguishing between different queries and their respective result sets. The actual interpretation and application of the criteria are handled by the ICascadeOrigin implementation.
The ICascadeOrigin is responsible for translating the criteria into a format that the underlying data source can understand. For example, if the Origin is a RESTful API, the criteria might be converted into query parameters. If it's a SQL database, the criteria could be translated into WHERE clauses.
The freshnessSeconds parameter plays a vital role in determining whether to execute the query or return cached results:
This mechanism allows for efficient caching of query results, reducing unnecessary network calls and database load.
Let's walk through an example:
The criteria object can be as simple or complex as needed. It could be a dictionary, an anonymous object, or a custom class. The ICascadeOrigin implementation is responsible for interpreting this object and applying it to the data source.
For instance, an Origin might support advanced querying features:
The Origin would then translate this into appropriate filtering logic for its data source.
The Query method in Cascade provides a powerful and flexible way to retrieve data, balancing efficiency through caching with the need for fresh data. By incorporating the criteria into the collection name and leveraging the ICascadeOrigin for actual data retrieval, Cascade offers a robust solution for querying data in various scenarios.