kandi background
kandi background
Explore Kits
kandi background
Explore Kits
kandi background
Explore Kits
kandi background
Explore Kits
Explore all Drupal open source software, libraries, packages, source code, cloud functions and APIs.

Popular New Releases in Drupal

v3.6.4

11.0.9

CMSeeK v.1.1.3 - K-RONA

v1.19.1: more compression options, ddev clean

Composer 2.1 compatibility

lando

v3.6.4

drush

11.0.9

CMSeeK

CMSeeK v.1.1.3 - K-RONA

ddev

v1.19.1: more compression options, ddev clean

composer-patches

Composer 2.1 compatibility

Popular Libraries in Drupal

Trending New libraries in Drupal

Top Authors in Drupal

1

21 Libraries

346

2

20 Libraries

2105

3

20 Libraries

68

4

19 Libraries

3843

5

18 Libraries

122

6

17 Libraries

868

7

16 Libraries

703

8

16 Libraries

194

9

16 Libraries

33

10

14 Libraries

320

1

21 Libraries

346

2

20 Libraries

2105

3

20 Libraries

68

4

19 Libraries

3843

5

18 Libraries

122

6

17 Libraries

868

7

16 Libraries

703

8

16 Libraries

194

9

16 Libraries

33

10

14 Libraries

320

Trending Kits in Drupal

No Trending Kits are available at this moment for Drupal

Trending Discussions on Drupal

    Not the required data is displayed for the new user, because the data is taken from the cache of the previous user
    Implement "cache tags pattern" in Spring
    Gatsby Source Drupal not fetching data when trying to deploy to netlify/heroku
    PHP Fatal error: Uncaught Error: Undefined constant "MB_OVERLOAD_STRING"
    Condense a Python Dictionary with Similar Keys
    React TS variable is undefined
    ERROR: the "PHPCompatibility" coding standard is not installed
    .htaccess drupal 7 RedirectMatch or RewriteRule nothing seams to work
    Does Drupal 7.87 support JQuery 3.6.x
    How to programmatically query the translated content of a block in drupal9

QUESTION

Not the required data is displayed for the new user, because the data is taken from the cache of the previous user

Asked 2022-Apr-10 at 10:14

I made a custom module that displays the weather in a particular city.

But I got these comments after the code review:

1. Interesting question, what happens to your cache data, if the site first comes to a person from the city of London, and then Paris?

As I understand it, it means that a person from Paris, when he enters the site, will see the weather in London, because it will be taken from the cache. But I put the data in the cache so that there are not too many requests, I made a request once, put the data in the cache, and the next time I took the data from the cache.

2. In the small function, you are calling the http://ip-api.com/json/ endpoint twice. What happens when the site is visited by a thousand people per minute?

Here, I do not understand what the problem is. If it meant that the connection limit to the resource would be exhausted, then how to solve this problem? In the getCity() function, put the data in the cache in the same way as I do in the build() function? But then the same problem comes up as in the first remark, if a person from another city visits the site, then the data from the cache (name of the city of London) will be taken and not the name of his real city. How then to be?

Can you please tell me what needs to be changed in my code? Below I will write a slightly reduced code for my php file. Full version here: https://phpsandbox.io/n/sweet-forest-1lew-1wmof

1// ....
2use Drupal\Core\Cache\CacheBackendInterface;
3use GuzzleHttp\Client;
4
5//....
6
7  public function getCity() {
8
9    $ip = '193.62.157.66';  // static because for testing
10
11    try {
12      $response_ip = $this->httpClient->get('http://ip-api.com/json/' . $ip);
13      $response_data_ip = $response_ip->getBody();
14      $data_ip = json_decode($response_data_ip);
15
16      if ($data_ip->status == 'success') {
17        return $data_ip->city;
18      }
19      else {
20        return $this->configFactory->get('sydneypro_weather.settings')->get('weather_city');
21      }
22
23    }
24    catch (RequestException $e) {
25      return FALSE;
26    }
27
28  }
29
30  public function build() {
31    $client = $this->httpClient;
32    $api_key = $this->configFactory->get('sydneypro_weather.settings')->get('weather_api_key');
33    $cid = 'sydneypro_weather';
34    $weather_config = $this->configFactory->get('sydneypro_weather.settings');
35
36    if (!$weather_config) {
37      $this->logger->get('sydneypro_weather')->error('Config "sydneypro_weather.settings" is missing4');
38      return [];
39    }
40
41    if (empty($api_key) || empty($this->getCity())) {
42      return [
43        '#type' => 'markup',
44        '#markup' => $this->t('Please enter your API key and City in the Admin panel to see the weather'),
45      ];
46    }
47
48    try {
49      if ($cache = $this->cacheBackend->get($cid)) {
50        $data = $cache->data;
51      }
52      else {
53        $response = $client->get('http://api.openweathermap.org/data/2.5/weather?q=' . $this->getCity() . ',&appid=' . $api_key . '&units=metric');
54        $response_data = $response->getBody();
55        $data = json_decode($response_data);
56        $this->cacheBackend->set($cid, $data, $this->time->getRequestTime() + 21600);
57      }
58
59      $build = [
60        '#theme' => 'weather_block',
61        '#data' => $data,
62        '#attached' => [
63          'library' => [
64            'sydneypro_weather/sydneypro_weather',
65          ],
66        ],
67      ];
68
69      return $build;
70
71    }
72  // ....
73
74

ANSWER

Answered 2022-Apr-10 at 10:14
  1. You need to display some data according to a given city, so you should cache the data on a per-city basis, using a cache id that allows you to retrieve data for a specific city. In other words, $cid must contain a city name or identifier.

copy icondownload icon

1// ....
2use Drupal\Core\Cache\CacheBackendInterface;
3use GuzzleHttp\Client;
4
5//....
6
7  public function getCity() {
8
9    $ip = '193.62.157.66';  // static because for testing
10
11    try {
12      $response_ip = $this->httpClient->get('http://ip-api.com/json/' . $ip);
13      $response_data_ip = $response_ip->getBody();
14      $data_ip = json_decode($response_data_ip);
15
16      if ($data_ip->status == 'success') {
17        return $data_ip->city;
18      }
19      else {
20        return $this->configFactory->get('sydneypro_weather.settings')->get('weather_city');
21      }
22
23    }
24    catch (RequestException $e) {
25      return FALSE;
26    }
27
28  }
29
30  public function build() {
31    $client = $this->httpClient;
32    $api_key = $this->configFactory->get('sydneypro_weather.settings')->get('weather_api_key');
33    $cid = 'sydneypro_weather';
34    $weather_config = $this->configFactory->get('sydneypro_weather.settings');
35
36    if (!$weather_config) {
37      $this->logger->get('sydneypro_weather')->error('Config "sydneypro_weather.settings" is missing4');
38      return [];
39    }
40
41    if (empty($api_key) || empty($this->getCity())) {
42      return [
43        '#type' => 'markup',
44        '#markup' => $this->t('Please enter your API key and City in the Admin panel to see the weather'),
45      ];
46    }
47
48    try {
49      if ($cache = $this->cacheBackend->get($cid)) {
50        $data = $cache->data;
51      }
52      else {
53        $response = $client->get('http://api.openweathermap.org/data/2.5/weather?q=' . $this->getCity() . ',&appid=' . $api_key . '&units=metric');
54        $response_data = $response->getBody();
55        $data = json_decode($response_data);
56        $this->cacheBackend->set($cid, $data, $this->time->getRequestTime() + 21600);
57      }
58
59      $build = [
60        '#theme' => 'weather_block',
61        '#data' => $data,
62        '#attached' => [
63          'library' => [
64            'sydneypro_weather/sydneypro_weather',
65          ],
66        ],
67      ];
68
69      return $build;
70
71    }
72  // ....
73
74$city = $this->getCity();
75$cid = 'sydneypro_weather:' . $city;
76
  • Using a variable for $city prevents getCity() to be called twice. Also you could set a cache that maps IP adresses to their corresponding city but it might not be a good idea as the number of (possible) distinct adresses populating the cache table could be very high.
    Another approach would be to get the region/city of the user from his browser using javascript and/or cookies, and call the api only for those that does not share their location.

  • Source https://stackoverflow.com/questions/71815094

    Community Discussions contain sources that include Stack Exchange Network

      Not the required data is displayed for the new user, because the data is taken from the cache of the previous user
      Implement "cache tags pattern" in Spring
      Gatsby Source Drupal not fetching data when trying to deploy to netlify/heroku
      PHP Fatal error: Uncaught Error: Undefined constant "MB_OVERLOAD_STRING"
      Condense a Python Dictionary with Similar Keys
      React TS variable is undefined
      ERROR: the "PHPCompatibility" coding standard is not installed
      .htaccess drupal 7 RedirectMatch or RewriteRule nothing seams to work
      Does Drupal 7.87 support JQuery 3.6.x
      How to programmatically query the translated content of a block in drupal9

    QUESTION

    Not the required data is displayed for the new user, because the data is taken from the cache of the previous user

    Asked 2022-Apr-10 at 10:14

    I made a custom module that displays the weather in a particular city.

    But I got these comments after the code review:

    1. Interesting question, what happens to your cache data, if the site first comes to a person from the city of London, and then Paris?

    As I understand it, it means that a person from Paris, when he enters the site, will see the weather in London, because it will be taken from the cache. But I put the data in the cache so that there are not too many requests, I made a request once, put the data in the cache, and the next time I took the data from the cache.

    2. In the small function, you are calling the http://ip-api.com/json/ endpoint twice. What happens when the site is visited by a thousand people per minute?

    Here, I do not understand what the problem is. If it meant that the connection limit to the resource would be exhausted, then how to solve this problem? In the getCity() function, put the data in the cache in the same way as I do in the build() function? But then the same problem comes up as in the first remark, if a person from another city visits the site, then the data from the cache (name of the city of London) will be taken and not the name of his real city. How then to be?

    Can you please tell me what needs to be changed in my code? Below I will write a slightly reduced code for my php file. Full version here: https://phpsandbox.io/n/sweet-forest-1lew-1wmof

    1// ....
    2use Drupal\Core\Cache\CacheBackendInterface;
    3use GuzzleHttp\Client;
    4
    5//....
    6
    7  public function getCity() {
    8
    9    $ip = '193.62.157.66';  // static because for testing
    10
    11    try {
    12      $response_ip = $this->httpClient->get('http://ip-api.com/json/' . $ip);
    13      $response_data_ip = $response_ip->getBody();
    14      $data_ip = json_decode($response_data_ip);
    15
    16      if ($data_ip->status == 'success') {
    17        return $data_ip->city;
    18      }
    19      else {
    20        return $this->configFactory->get('sydneypro_weather.settings')->get('weather_city');
    21      }
    22
    23    }
    24    catch (RequestException $e) {
    25      return FALSE;
    26    }
    27
    28  }
    29
    30  public function build() {
    31    $client = $this->httpClient;
    32    $api_key = $this->configFactory->get('sydneypro_weather.settings')->get('weather_api_key');
    33    $cid = 'sydneypro_weather';
    34    $weather_config = $this->configFactory->get('sydneypro_weather.settings');
    35
    36    if (!$weather_config) {
    37      $this->logger->get('sydneypro_weather')->error('Config "sydneypro_weather.settings" is missing4');
    38      return [];
    39    }
    40
    41    if (empty($api_key) || empty($this->getCity())) {
    42      return [
    43        '#type' => 'markup',
    44        '#markup' => $this->t('Please enter your API key and City in the Admin panel to see the weather'),
    45      ];
    46    }
    47
    48    try {
    49      if ($cache = $this->cacheBackend->get($cid)) {
    50        $data = $cache->data;
    51      }
    52      else {
    53        $response = $client->get('http://api.openweathermap.org/data/2.5/weather?q=' . $this->getCity() . ',&appid=' . $api_key . '&units=metric');
    54        $response_data = $response->getBody();
    55        $data = json_decode($response_data);
    56        $this->cacheBackend->set($cid, $data, $this->time->getRequestTime() + 21600);
    57      }
    58
    59      $build = [
    60        '#theme' => 'weather_block',
    61        '#data' => $data,
    62        '#attached' => [
    63          'library' => [
    64            'sydneypro_weather/sydneypro_weather',
    65          ],
    66        ],
    67      ];
    68
    69      return $build;
    70
    71    }
    72  // ....
    73
    74

    ANSWER

    Answered 2022-Apr-10 at 10:14
    1. You need to display some data according to a given city, so you should cache the data on a per-city basis, using a cache id that allows you to retrieve data for a specific city. In other words, $cid must contain a city name or identifier.

    copy icondownload icon

    1// ....
    2use Drupal\Core\Cache\CacheBackendInterface;
    3use GuzzleHttp\Client;
    4
    5//....
    6
    7  public function getCity() {
    8
    9    $ip = '193.62.157.66';  // static because for testing
    10
    11    try {
    12      $response_ip = $this->httpClient->get('http://ip-api.com/json/' . $ip);
    13      $response_data_ip = $response_ip->getBody();
    14      $data_ip = json_decode($response_data_ip);
    15
    16      if ($data_ip->status == 'success') {
    17        return $data_ip->city;
    18      }
    19      else {
    20        return $this->configFactory->get('sydneypro_weather.settings')->get('weather_city');
    21      }
    22
    23    }
    24    catch (RequestException $e) {
    25      return FALSE;
    26    }
    27
    28  }
    29
    30  public function build() {
    31    $client = $this->httpClient;
    32    $api_key = $this->configFactory->get('sydneypro_weather.settings')->get('weather_api_key');
    33    $cid = 'sydneypro_weather';
    34    $weather_config = $this->configFactory->get('sydneypro_weather.settings');
    35
    36    if (!$weather_config) {
    37      $this->logger->get('sydneypro_weather')->error('Config "sydneypro_weather.settings" is missing4');
    38      return [];
    39    }
    40
    41    if (empty($api_key) || empty($this->getCity())) {
    42      return [
    43        '#type' => 'markup',
    44        '#markup' => $this->t('Please enter your API key and City in the Admin panel to see the weather'),
    45      ];
    46    }
    47
    48    try {
    49      if ($cache = $this->cacheBackend->get($cid)) {
    50        $data = $cache->data;
    51      }
    52      else {
    53        $response = $client->get('http://api.openweathermap.org/data/2.5/weather?q=' . $this->getCity() . ',&appid=' . $api_key . '&units=metric');
    54        $response_data = $response->getBody();
    55        $data = json_decode($response_data);
    56        $this->cacheBackend->set($cid, $data, $this->time->getRequestTime() + 21600);
    57      }
    58
    59      $build = [
    60        '#theme' => 'weather_block',
    61        '#data' => $data,
    62        '#attached' => [
    63          'library' => [
    64            'sydneypro_weather/sydneypro_weather',
    65          ],
    66        ],
    67      ];
    68
    69      return $build;
    70
    71    }
    72  // ....
    73
    74$city = $this->getCity();
    75$cid = 'sydneypro_weather:' . $city;
    76
  • Using a variable for $city prevents getCity() to be called twice. Also you could set a cache that maps IP adresses to their corresponding city but it might not be a good idea as the number of (possible) distinct adresses populating the cache table could be very high.
    Another approach would be to get the region/city of the user from his browser using javascript and/or cookies, and call the api only for those that does not share their location.

  • Source https://stackoverflow.com/questions/71815094

    Community Discussions contain sources that include Stack Exchange Network

    Tutorials and Learning Resources in Drupal

    Tutorials and Learning Resources are not available at this moment for Drupal

    Share this Page

    share link

    Get latest updates on Drupal

    • © 2022 Open Weaver Inc.