-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi_cache.php
More file actions
53 lines (44 loc) · 2.57 KB
/
api_cache.php
File metadata and controls
53 lines (44 loc) · 2.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<?php
//Will Optimize it
$api_id = 'f54f0db5';
$api_key = '94527f02437544cd169ac13168907cd8';
// Prepare the API endpoint URL with query parameters
$search_query = isset($_GET['q']) ? urlencode($_GET['q']) : 'chicken'; // Default search query is 'chicken'
$apiEndpoint = 'https://api.edamam.com/search?q=' . $search_query . '&app_id=' . $api_id . '&app_key=' . $api_key;
//Initialize Memcache
$cache = new MemCached();
$cache -> addServer('localhost',11211); // Memecache port
// Function to fetch API data with caching
function fetchDataFromApi($apiEndpoint, $requestParameters) {
global $cache;
// Create a unique cache key based on the API endpoint and request parameters
$cacheKey = md5($apiEndpoint . serialize($requestParameters));
echo "Cache Key: " . $cacheKey . "<br>";
// Check if data exists in the cache
$cachedData = $cache->get($cacheKey);
if (!$cachedData) {
// Data not found in the cache, make the API request
// Assuming you have an API client function named makeApiRequest
$apiResponse = makeApiRequest($apiEndpoint, $requestParameters);
// Store the API response in the cache with an expiration time (e.g., 1 hour)
// (Memcached Protocol)
$cache->set($cacheKey, $apiResponse, 3600); // Expiration time of 1 hour
} else {
// Data found in the cache, use it directly
$apiResponse = $cachedData;
}
return $apiResponse;
}
echo '<input type="hidden" name="meal_name" value="' . $favoriteMeal['meal_name'] . '">';
echo '<input type="hidden" name="calories" value="' . $favoriteMeal['calories'] . '">';
echo '<input type="hidden" name="fat" value="' . $favoriteMeal['fat'] . '">';
echo '<input type="hidden" name="protein" value="' . $favoriteMeal['protein'] . '">';
echo '<input type="hidden" name="carbohydrates" value="' . $favoriteMeal['carbohydrates'] . '">';
echo '<button type="submit"><i class="fas fa-check"></i> Finished</button>';
echo '</form>';
// Second form for the "Delete" action
echo '<form method="post" action="delete-favorite.php">';
echo '<input type="hidden" name="id" value="' . $favoriteMeal['id'] . '">';
echo '<button class="delete" type="submit"><i class="fas fa-times"></i> Delete</button>';
echo '</form>';
?>