Skip to content

Commit

Permalink
Improve cache
Browse files Browse the repository at this point in the history
  • Loading branch information
turbo124 committed Apr 24, 2020
1 parent 1320c20 commit 7c45cbc
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 14 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
[![Latest Version on Packagist](https://img.shields.io/packagist/v/turbo124/collector.svg?style=flat-square)](https://packagist.org/packages/turbo124/collector)
[![Total Downloads](https://img.shields.io/packagist/dt/turbo124/collector.svg?style=flat-square)](https://packagist.org/packages/turbo124/collector)

Application level offsite metrics collector for laravel.
Application offsite metrics collector for Laravel.

## Installation

Expand Down
32 changes: 21 additions & 11 deletions src/Collector.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,38 +17,38 @@ public function __construct()

public function create($metric)
{
$this->metric = $metric;
$this->metric->datetime = date("Y-m-d H:i:s");
$this->metric->api_key = config('collector.api_key');
$this->metric = $metric;
$this->metric->datetime = date("Y-m-d H:i:s");
$this->metric->api_key = config('collector.api_key');

return $this;
}

public function increment()
{
$this->metric->counter++;
$this->metric->counter++;

return $this;
return $this;
}

public function decrement()
{
$this->metric->counter--;
$this->metric->counter--;

return $this;
return $this;
}

public function setCount($count)
{
$this->metric->counter = $count;
$this->metric->counter = $count;

return $this;
return $this;
}

public function send()
{
$generator = new Generator();
$generator->fire($this->metric);
$generator->fire($this->metric);
}

public function queue()
Expand All @@ -60,6 +60,16 @@ public function batch()
{
config(['cache.default' => 'array']);

Cache::tags(['collector'])->put(config('collector.api_key'), $this->metric);
$data = Cache::tags(['collector'])->get('collector');

if(is_array($data)){
$data[] = $this->metric;
}
else {
$data = [];
$data[] = $this->metric;
}

Cache::tags(['collector'])->put('collector', $data);
}
}
2 changes: 1 addition & 1 deletion src/Jobs/BatchMetrics.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function handle()

config(['cache.default' => 'array']);

$metrics = Cache::tags(['collector'])->get(config('collector.api_key'));
$metrics = Cache::tags(['collector'])->get('collector');

$generator = new Generator();
$generator->batchFire($metrics);
Expand Down
38 changes: 38 additions & 0 deletions tests/CacheTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace Turbo124\Collector\Tests;

use Illuminate\Support\Facades\Cache;
use Orchestra\Testbench\TestCase;
use Turbo124\Collector\Collector;
use Turbo124\Collector\CollectorServiceProvider;
use Turbo124\Collector\Collector\Generator;

class CacheTest extends TestCase
{
/** @test */
public function testCacheGetAndPut()
{
$test_array = ['a' => 'b', 'c'=>'d'];

config(['cache.default' => 'array']);

$data = Cache::tags(['collector'])->get('collector');

if(is_array($data)){
$data[] = $test_array;
}
else {
$data = [];
$data[] = $test_array;
}

Cache::tags(['collector'])->put('collector', $data);

$test_data = Cache::tags(['collector'])->get('collector');

$this->assertTrue(is_array($test_data));

$this->assertEquals($test_data[0]['a'], 'b');
}
}
2 changes: 1 addition & 1 deletion tests/ConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ class ConfigTest extends TestCase
public function testValidInstanceType()
{
$collector = new Collector;
$this->assertTrue($collector->create() instanceof Collector);
$this->assertTrue($collector instanceof Collector);
}
}

0 comments on commit 7c45cbc

Please sign in to comment.