Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Campaign] Add phpredis support #232

Open
wants to merge 22 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
4df146b
Configure using one time banners, configure persistent redis connection
pulzarraider Oct 5, 2023
855f27f
Fetch all running campaigns at once with `mget`
pulzarraider Oct 6, 2023
3115ed9
Optimize loading maps with `mget`
pulzarraider Oct 6, 2023
8e0ea94
Parse the user agent just once
pulzarraider Oct 6, 2023
596999a
Check segment cache only once
pulzarraider Oct 6, 2023
0e8f43e
Check user or browser in same segment just once
pulzarraider Oct 6, 2023
8844388
Store result of device detection in redis
pulzarraider Oct 7, 2023
cbe89f4
Fix error `Too few arguments to function PlainPhpShowtimeResponse::su…
pulzarraider Oct 9, 2023
986f2ac
Add phpredis support
pulzarraider Oct 9, 2023
3772b1e
Fix phpredis compatibility
pulzarraider Oct 9, 2023
3184363
Refresh redis instance after deserialization to avoid duplicated conn…
pulzarraider Oct 9, 2023
27d3514
Refresh redis instance in `Segment` after deserialization to avoid du…
pulzarraider Oct 10, 2023
defad10
Merge main optimization branch
pulzarraider Oct 10, 2023
66187a6
Code cleanup
pulzarraider Oct 10, 2023
1fcd2c5
Store device detection to local cache
pulzarraider Oct 10, 2023
b9b918e
Store device detection to local cache
pulzarraider Oct 10, 2023
bf12523
Merge branch 'configure_one_time_banner' into phpredis
pulzarraider Oct 10, 2023
0ec4b93
Fix tests
pulzarraider Oct 10, 2023
04161ec
Merge branch 'configure_one_time_banner' into phpredis
pulzarraider Oct 10, 2023
4cdb7f3
Merge master
pulzarraider Oct 27, 2023
8aba3c9
Rollback default value in ShowtimeResponse::success
pulzarraider Oct 27, 2023
c843942
Merge branch 'configure_one_time_banner' into phpredis
pulzarraider Oct 27, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Configure using one time banners, configure persistent redis connection
  • Loading branch information
pulzarraider committed Oct 5, 2023
commit 4df146bdfe81c150bb91bedfc7b1d9d9ad9cdf26
10 changes: 10 additions & 0 deletions Campaign/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ REDIS_HOST=redis
# Redis connection port. 6379 is the default port used by Redis installation.
REDIS_PORT=6379

# Specifies if the underlying redis connection resource should be left open when a script ends its lifecycle. Default: false
REDIS_PERSISTENT=false

# Alternative Redis connection configuration.
# The accepted format of a single connection is
#
Expand Down Expand Up @@ -249,3 +252,10 @@ MAXMIND_DATABASE=resources/assets/maxmind/GeoLite2-Country.mmdb
# In the case of same amount of variants the banner from more recent campaign is prioritized.

#PRIORITIZE_BANNERS_ON_SAME_POSITION=

# Flag whether the one time banners should be enabled or not.
#
# If this option is set to `true`, the user and browser one time banners are used in Showtime.
# If you are not using the one time banners, set this option to `false` for better performance.
# The default value is `true`.
ONE_TIME_BANNER_ENABLED=true
24 changes: 13 additions & 11 deletions Campaign/app/Http/Showtime/Showtime.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,17 +158,19 @@ public function showtime(string $userData, string $callback, ShowtimeResponse $s

$displayData = [];

// Try to load one-time banners (having precedence over campaigns)
$banner = null;
if ($userId) {
$banner = $this->loadOneTimeUserBanner($userId);
}
if (!$banner) {
$banner = $this->loadOneTimeBrowserBanner($browserId);
}
if ($banner) {
$displayData[] = $showtimeResponse->renderBanner($banner, $alignments, $dimensions, $positions, $snippets);
return $showtimeResponse->success($callback, $displayData, [], $segmentAggregator->getProviderData());
if ($this->showtimeConfig->isOneTimeBannerEnabled()) {
// Try to load one-time banners (having precedence over campaigns)
$banner = null;
if ($userId) {
$banner = $this->loadOneTimeUserBanner($userId);
}
if (!$banner) {
$banner = $this->loadOneTimeBrowserBanner($browserId);
}
if ($banner) {
$displayData[] = $showtimeResponse->renderBanner($banner, $alignments, $dimensions, $positions, $snippets);
return $showtimeResponse->success($callback, $displayData, [], $segmentAggregator->getProviderData());
}
}

$campaignIds = json_decode($this->redis->get(Campaign::ACTIVE_CAMPAIGN_IDS) ?? '[]') ?? [];
Expand Down
14 changes: 14 additions & 0 deletions Campaign/app/Http/Showtime/ShowtimeConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ class ShowtimeConfig

private bool $prioritizeBannerOnSamePosition = false;

private bool $oneTimeBannerEnabled = true;

public function setAcceptLanguage(string $language): void
{
$this->acceptLanguage = $language;
Expand All @@ -27,4 +29,16 @@ public function setPrioritizeBannerOnSamePosition(bool $prioritizeBannerOnSamePo
{
$this->prioritizeBannerOnSamePosition = $prioritizeBannerOnSamePosition;
}

public function isOneTimeBannerEnabled(): bool
{
return $this->oneTimeBannerEnabled;
}

public function setOneTimeBannerEnabled(bool $oneTimeBannerEnabled): self
{
$this->oneTimeBannerEnabled = $oneTimeBannerEnabled;

return $this;
}
}
5 changes: 5 additions & 0 deletions Campaign/app/Providers/AppServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use App\Contracts\SegmentAggregator;
use App\Http\Resources\SearchResource;
use App\Http\Showtime\LazyGeoReader;
use App\Http\Showtime\ShowtimeConfig;
use Illuminate\Database\Connection;
use Illuminate\Foundation\Application;
use Illuminate\Pagination\Paginator;
Expand Down Expand Up @@ -47,6 +48,10 @@ public function register()
return new LazyGeoReader(config("services.maxmind.database"));
});

$this->app->bind(ShowtimeConfig::class, function () {
return (new ShowtimeConfig())->setOneTimeBannerEnabled(config("banners.one_time_banner_enabled"));
});

$this->app->bind(SegmentAggregator::class, function (Application $app) {
return new SegmentAggregator($app->tagged(SegmentAggregator::TAG));
});
Expand Down
3 changes: 2 additions & 1 deletion Campaign/config/banners.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,5 +91,6 @@
],
],
],
'prioritize_banners_on_same_position' => env('PRIORITIZE_BANNERS_ON_SAME_POSITION', false)
'prioritize_banners_on_same_position' => env('PRIORITIZE_BANNERS_ON_SAME_POSITION', false),
'one_time_banner_enabled' => env('ONE_TIME_BANNER_ENABLED', true)
];
1 change: 1 addition & 0 deletions Campaign/config/database.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ function configure_redis($database)
'password' => env('REDIS_PASSWORD'),
'port' => env('REDIS_PORT', '6379'),
'database' => $database,
'persistent' => env('REDIS_PERSISTENT', false),
];
}
}
Expand Down
2 changes: 2 additions & 0 deletions Campaign/public/showtime.php
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,7 @@ private function renderInternal(
'port' => env('REDIS_PORT') ?: 6379,
'password' => env('REDIS_PASSWORD') ?: null,
'database' => env('REDIS_DEFAULT_DATABASE') ?: 0,
'persistent' => env('REDIS_PERSISTENT', false),
], [
'prefix' => env('REDIS_PREFIX') ?: '',
]);
Expand All @@ -457,6 +458,7 @@ private function renderInternal(
['options' => ['default' => false]]
);
$showtimeConfig->setPrioritizeBannerOnSamePosition($prioritizeBannerOnSamePosition);
$showtimeConfig->setOneTimeBannerEnabled(env('ONE_TIME_BANNER_ENABLED', true));

$showtime = new Showtime($redis, $segmentAggregator, $geoReader, $showtimeConfig, $deviceDetector, $logger);
$showtime->showtime($data, $callback, $showtimeResponse);
Expand Down