Jump to content

Using \IPS\Data\Store for weather array


clearvision

Recommended Posts

Posted

I've searched here and with Google and still don't have a clear picture of the persistence of  \IPS\Data\Store::i()->myvar.

Some have said it is cached and could go away, others suggest using it for storing plugin variables.  If cached how often is it cleared?

I'm using it to store weather data based on my user's location.  When a user comes to site, if location is not in stored array, I fetch from weather provider, and store in an array with location,temp,icon,time.  I then use this to generate weather info under everyone's profile.  Need to store data to reduce calls to provider for performance and request limit reasons.  I also check time of last update in array to avoid fetching from provider on every page access.

If the data were to go away every once in while, it will slowly rebuild, but I want to handle that case and fetch extra data.  Also if it never clears then I need to do housekeeping on old results.

weather.thumb.JPG.0e9d6545f482efd498d28c81b89ecc78.JPG

Posted

you can use in-build cache system

/*	Get the result, reduce calls */
try
{
	$result = \IPS\Data\Cache::i()->getWithExpire( 'wheather_today', TRUE );
}
catch ( \OutOfRangeException $e )
{
	$result = \IPS\Http\Url::external( $endpoint )
				->setQueryString( array(
					'format'	=> 'json',
					'url'		=> (string) $url->stripQueryString( 'autoplay' ),
					'scheme'	=> ( $url->data['scheme'] === 'https' or \IPS\Request::i()->isSecure() ) ? 'https' : null
				) )
				->request( static::$requestTimeout )
				->setHeaders( array( 'Accept-Language' => \IPS\Member::loggedIn()->language()->short ) )
				->get();
	/* Store result into cache system with expiration */
	\IPS\Data\Cache::i()->storeWithExpire( 'wheather_today', $result, \IPS\DateTime::create()->add( new \DateInterval( 'P1D' ) ), TRUE );
}

 

Archived

This topic is now archived and is closed to further replies.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...