Skip to content

Commit

Permalink
add api request caching
Browse files Browse the repository at this point in the history
  • Loading branch information
michavie committed Oct 29, 2021
1 parent 14f3c65 commit 62b1cac
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 13 deletions.
6 changes: 4 additions & 2 deletions src/Api/Addresses/AddressEndpoints.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,22 @@

namespace Superciety\ElrondSdk\Api\Addresses;

use Carbon\Carbon;
use Superciety\ElrondSdk\Api\EndpointBase;
use Superciety\ElrondSdk\Api\Addresses\Responses\Address;

class AddressEndpoints extends EndpointBase
{
public function __construct(
private string $baseUrl
private string $baseUrl,
private ?Carbon $cacheTtl,
) {
}

public function getAddress(string $address): Address
{
return Address::fromResponse(
static::request('GET', "{$this->baseUrl}/addresses/{$address}", skipDataUnwrapping: true)
static::request('GET', "{$this->baseUrl}/addresses/{$address}", $this->cacheTtl, skipDataUnwrapping: true)
);
}
}
15 changes: 12 additions & 3 deletions src/Api/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Superciety\ElrondSdk\Api;

use Exception;
use Carbon\Carbon;
use Superciety\ElrondSdk\Api\Blocks\BlockEndpoints;
use Superciety\ElrondSdk\Api\Network\NetworkEndpoints;
use Superciety\ElrondSdk\Api\Addresses\AddressEndpoints;
Expand All @@ -14,6 +15,7 @@ final class Api
const DevnetApiBaseUrl = 'https://devnet-api.elrond.com';

private string $apiBaseUrl;
private ?Carbon $cacheTtl = null;

public function __construct(string $chain)
{
Expand All @@ -25,18 +27,25 @@ public function __construct(string $chain)
};
}

public function cacheFor(Carbon $ttl): self
{
$this->cacheTtl = $ttl;

return $this;
}

public function addresses(): AddressEndpoints
{
return new AddressEndpoints($this->apiBaseUrl);
return new AddressEndpoints($this->apiBaseUrl, $this->cacheTtl);
}

public function network(): NetworkEndpoints
{
return new NetworkEndpoints($this->apiBaseUrl);
return new NetworkEndpoints($this->apiBaseUrl, $this->cacheTtl);
}

public function blocks(): BlockEndpoints
{
return new BlockEndpoints($this->apiBaseUrl);
return new BlockEndpoints($this->apiBaseUrl, $this->cacheTtl);
}
}
8 changes: 5 additions & 3 deletions src/Api/Blocks/BlockEndpoints.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,29 @@

namespace Superciety\ElrondSdk\Api\Blocks;

use Carbon\Carbon;
use Superciety\ElrondSdk\Api\EndpointBase;
use Superciety\ElrondSdk\Api\Blocks\Responses\Hyperblock;

final class BlockEndpoints extends EndpointBase
{
public function __construct(
private string $baseUrl
private string $baseUrl,
private ?Carbon $cacheTtl,
) {
}

public function getHyperblockByNonce(string $nonce): Hyperblock
{
return Hyperblock::fromResponse(
static::request('GET', "{$this->baseUrl}/hyperblock/by-nonce/{$nonce}")['hyperblock']
static::request('GET', "{$this->baseUrl}/hyperblock/by-nonce/{$nonce}", $this->cacheTtl)['hyperblock']
);
}

public function getHyperblockByHash(string $nonce): Hyperblock
{
return Hyperblock::fromResponse(
static::request('GET', "{$this->baseUrl}/hyperblock/by-hash/{$nonce}")['hyperblock']
static::request('GET', "{$this->baseUrl}/hyperblock/by-hash/{$nonce}", $this->cacheTtl)['hyperblock']
);
}
}
15 changes: 14 additions & 1 deletion src/Api/EndpointBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,29 @@

namespace Superciety\ElrondSdk\Api;

use Carbon\Carbon;
use Illuminate\Support\Str;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Cache;

abstract class EndpointBase
{
protected static function request(string $method, string $url, bool $skipDataUnwrapping = false): array
protected static function request(string $method, string $url, ?Carbon $cacheTtl, bool $skipDataUnwrapping = false): array
{
$cacheKey = Str::lower("{$method}-{$url}");

if ($cacheTtl && $cached = Cache::get($cacheKey)) {
return $cached;
}

$res = Http::send($method, $url)
->throw()
->json();

if ($cacheTtl) {
Cache::put($cacheKey, $res, $cacheTtl);
}

return $skipDataUnwrapping ? $res : $res['data'];
}
}
10 changes: 6 additions & 4 deletions src/Api/Network/NetworkEndpoints.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Superciety\ElrondSdk\Api\Network;

use Carbon\Carbon;
use Superciety\ElrondSdk\Api\EndpointBase;
use Superciety\ElrondSdk\Api\Network\Responses\Economics;
use Superciety\ElrondSdk\Api\Network\Responses\ShardStatus;
Expand All @@ -10,28 +11,29 @@
final class NetworkEndpoints extends EndpointBase
{
public function __construct(
private string $baseUrl
private string $baseUrl,
private ?Carbon $cacheTtl,
) {
}

public function getEconomics(): Economics
{
return Economics::fromResponse(
static::request('GET', "{$this->baseUrl}/network/economics")['metrics']
static::request('GET', "{$this->baseUrl}/network/economics", $this->cacheTtl)['metrics']
);
}

public function getNetworkConfig(): NetworkConfig
{
return NetworkConfig::fromResponse(
static::request('GET', "{$this->baseUrl}/network/config")['config']
static::request('GET', "{$this->baseUrl}/network/config", $this->cacheTtl)['config']
);
}

public function getShardStatus(int $shardId): ShardStatus
{
return ShardStatus::fromResponse(
static::request('GET', "{$this->baseUrl}/network/status/{$shardId}")['status']
static::request('GET', "{$this->baseUrl}/network/status/{$shardId}", $this->cacheTtl)['status']
);
}
}

0 comments on commit 62b1cac

Please sign in to comment.