Cache keys are generated based on specific attributes of the data being cached. When a request is made for data, the system checks if a cache entry exists for the corresponding cache key. If a match is found, the cached data is returned, bypassing the need for a database query. If no match is found, the system retrieves the data from the database, stores it in the cache, and associates it with the generated cache key.