Skip to content

Commit

Permalink
Add missing cache invalidations to tank model
Browse files Browse the repository at this point in the history
  • Loading branch information
Difegue committed Dec 28, 2024
1 parent 08fed04 commit 3cca1e0
Showing 1 changed file with 43 additions and 42 deletions.
85 changes: 43 additions & 42 deletions lib/LANraragi/Model/Tankoubon.pm
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,11 @@ use Redis;
use Mojo::JSON qw(decode_json encode_json);
use List::Util qw(min);

use LANraragi::Utils::Database qw(redis_encode redis_decode invalidate_cache get_archive_json_multi get_tankoubons_by_file);
use LANraragi::Utils::Generic qw(array_difference filter_hash_by_keys);
use LANraragi::Utils::Logging qw(get_logger);
use LANraragi::Utils::Database qw(redis_encode redis_decode invalidate_cache get_archive_json_multi get_tankoubons_by_file);
use LANraragi::Utils::Generic qw(array_difference filter_hash_by_keys);
use LANraragi::Utils::Logging qw(get_logger);

my %TANK_METADATA = (
"name", 0,
"summary", -1,
"tags", -2
);
my %TANK_METADATA = ( "name", 0, "summary", -1, "tags", -2 );

# get_tankoubon_list(page)
# Returns a list of all the Tankoubon objects.
Expand Down Expand Up @@ -87,7 +83,7 @@ sub create_tankoubon ( $name, $tank_id ) {
my @old_name = $redis->zrangebyscore( $tank_id, 0, 0, qw{LIMIT 0 1} );
my $n = redis_decode( $old_name[0] );

if ($redis->exists($tank_id)) {
if ( $redis->exists($tank_id) ) {
$redis->zrem( $tank_id, $n );
}
}
Expand All @@ -96,16 +92,17 @@ sub create_tankoubon ( $name, $tank_id ) {
# Score 0 will be reserved for the name of the tank
my $tank_title = redis_encode($name);

# Add the tank name to LRR_TITLES so it shows up in tagless searches when tank grouping is enabled.
# Add the tank name to LRR_TITLES so it shows up in tagless searches when tank grouping is enabled.
$redis_search->zadd( "LRR_TITLES", 0, "$tank_title\0$tank_id" );

# Init metadata
$redis->zadd( $tank_id, $TANK_METADATA{"name"}, redis_encode("name_${tank_title}") );
$redis->zadd( $tank_id, $TANK_METADATA{"name"}, redis_encode("name_${tank_title}") );
$redis->zadd( $tank_id, $TANK_METADATA{"summary"}, "summary_" );
$redis->zadd( $tank_id, $TANK_METADATA{"tags"}, "tags_" );
$redis->zadd( $tank_id, $TANK_METADATA{"tags"}, "tags_" );

$redis->quit;
$redis_search->quit;
invalidate_cache();

return $tank_id;
}
Expand All @@ -132,10 +129,10 @@ sub get_tankoubon ( $tank_id, $fulldata = 0, $page = 0 ) {
}

# Declare some needed variables
my @allowed_keys = ('name', 'summary', 'tags', 'archives', 'full_data', 'id');
my @allowed_keys = ( 'name', 'summary', 'tags', 'archives', 'full_data', 'id' );
my @archives;
my @limit = split( ' ', "LIMIT " . ( $keysperpage * $page ) . " $keysperpage" );
my %tank = fetch_metadata_fields($tank_id);
my %tank = fetch_metadata_fields($tank_id);

my %tankoubon;

Expand Down Expand Up @@ -167,7 +164,7 @@ sub get_tankoubon ( $tank_id, $fulldata = 0, $page = 0 ) {
# Add the key as well
$tank{id} = $tank_id;

%tank = filter_hash_by_keys(\@allowed_keys, %tank);
%tank = filter_hash_by_keys( \@allowed_keys, %tank );

my $total = $redis->zcard($tank_id) - 1;

Expand All @@ -193,11 +190,14 @@ sub delete_tankoubon ($tank_id) {

if ( $redis->exists($tank_id) ) {
$redis->del($tank_id);

# The ID will remain in LRR_TITLES until the next stats compute, but this'll prevent it from appearing in search.
$redis_search->srem( "LRR_TANKGROUPED", $tank_id );
$redis_search->srem( "LRR_TANKGROUPED", $tank_id );

$redis->quit;
$redis_search->quit;
invalidate_cache();

return 1;
} else {
$logger->warn("$tank_id doesn't exist in the database!");
Expand All @@ -216,24 +216,24 @@ sub update_tankoubon ( $tank_id, $data ) {
my ( $result, $err ) = update_archive_list( $tank_id, $data );
}

return ( $result, $err);
return ( $result, $err );
}

# update_metadata(tankoubonid, data)
# Updates the metadata in the Tankoubon.
# Returns 1 on success, 0 on failure alongside an error message.
sub update_metadata( $tank_id, $data ) {
sub update_metadata ( $tank_id, $data ) {

if ( not defined $data->{"metadata"} ) {
return ( 1, "" );
}

my $logger = get_logger( "Tankoubon", "lanraragi" );
my $redis = LANraragi::Model::Config->get_redis;
my $err = "";
my $name = $data->{"metadata"}->{"name"} || undef;
my $summary = $data->{"metadata"}->{"summary"} || undef;
my $tags = $data->{"metadata"}->{"tags"} || undef;
my $logger = get_logger( "Tankoubon", "lanraragi" );
my $redis = LANraragi::Model::Config->get_redis;
my $err = "";
my $name = $data->{"metadata"}->{"name"} || undef;
my $summary = $data->{"metadata"}->{"summary"} || undef;
my $tags = $data->{"metadata"}->{"tags"} || undef;

if ( $redis->exists($tank_id) ) {
if ( defined $name ) {
Expand All @@ -256,7 +256,8 @@ sub update_metadata( $tank_id, $data ) {

$err = "$tank_id doesn't exist in the database!";
$logger->warn($err);
$redis->quit;

invalidate_cache();
return ( 0, $err );
}

Expand Down Expand Up @@ -299,8 +300,8 @@ sub update_archive_list ( $tank_id, $data ) {

# Make removed archives visible in search again unless other tanks contain them
foreach my $arc_id (@diff) {
unless (get_tankoubons_containing_archive($arc_id)) {
$redis_search->sadd( "LRR_TANKGROUPED", $arc_id );
unless ( get_tankoubons_containing_archive($arc_id) ) {
$redis_search->sadd( "LRR_TANKGROUPED", $arc_id );
}
}
}
Expand All @@ -316,8 +317,9 @@ sub update_archive_list ( $tank_id, $data ) {
for ( my $i = 0; $i < $len; $i = $i + 1 ) {
push @update, $i + 1;
push @update, $tank_archives[$i];

# Remove the ID if present, as it's been absorbed into the tank
$redis_search->srem( "LRR_TANKGROUPED", $tank_archives[$i] );
$redis_search->srem( "LRR_TANKGROUPED", $tank_archives[$i] );
}

# Update
Expand All @@ -330,7 +332,6 @@ sub update_archive_list ( $tank_id, $data ) {
$redis_search->quit;

invalidate_cache();

return ( 1, $err );
}

Expand Down Expand Up @@ -372,11 +373,11 @@ sub add_to_tankoubon ( $tank_id, $arc_id ) {

# Adding an archive to the tank will always hide it from main search, and show the tank instead
$redis = LANraragi::Model::Config->get_redis_search;
$redis->srem( "LRR_TANKGROUPED", $arc_id );
$redis->sadd( "LRR_TANKGROUPED", $tank_id ); # Set elements are unique so no problem if the tank is already added here
$redis->srem( "LRR_TANKGROUPED", $arc_id );
$redis->sadd( "LRR_TANKGROUPED", $tank_id ); # Set elements are unique so no problem if the tank is already added here
$redis->quit;
invalidate_cache();

invalidate_cache();
return ( 1, $err );
}

Expand Down Expand Up @@ -433,21 +434,22 @@ sub remove_from_tankoubon ( $tank_id, $arcid ) {
$redis->zadd( $tank_id, @update );
}

if ($redis->zcard($tank_id) == 1) {
if ( $redis->zcard($tank_id) == 1 ) {

# No elements in tank, remove it from search
$redis->srem( "LRR_TANKGROUPED", $tank_id );
}

$redis->quit;

# Removing an archive from a tank might have it show up in main search again
unless (get_tankoubons_containing_archive($arcid)) {
unless ( get_tankoubons_containing_archive($arcid) ) {
$redis = LANraragi::Model::Config->get_redis_search;
$redis->sadd( "LRR_TANKGROUPED", $arcid );
$redis->quit;
}
invalidate_cache();

invalidate_cache();
return ( 1, $err );
}

Expand Down Expand Up @@ -489,20 +491,20 @@ sub get_tankoubons_containing_archive ($arcid) {
}

sub update_metadata_field ( $tank_id, $field, $value ) {
my $redis = LANraragi::Model::Config->get_redis;
my $redis = LANraragi::Model::Config->get_redis;

$redis->zremrangebyscore( $tank_id, $TANK_METADATA{$field}, $TANK_METADATA{$field} );
$redis->zadd( $tank_id, $TANK_METADATA{$field}, redis_encode("${field}_${value}") );

return 1;
}

sub fetch_metadata_fields ( $tank_id ) {
my $redis = LANraragi::Model::Config->get_redis;
sub fetch_metadata_fields ($tank_id) {
my $redis = LANraragi::Model::Config->get_redis;

# Fetch from DB
my @keys = sort { $TANK_METADATA{$a} <=> $TANK_METADATA{$b} } keys(%TANK_METADATA);
my @raw_values = $redis->zrangebyscore( $tank_id, $TANK_METADATA{$keys[0]}, 0 );
my @keys = sort { $TANK_METADATA{$a} <=> $TANK_METADATA{$b} } keys(%TANK_METADATA);
my @raw_values = $redis->zrangebyscore( $tank_id, $TANK_METADATA{ $keys[0] }, 0 );

# Clean the data
my %metadata;
Expand All @@ -512,13 +514,12 @@ sub fetch_metadata_fields ( $tank_id ) {
my $clean_value = redis_decode($raw_value) || "";
$clean_value =~ s/^$key\_//;
$metadata{$key} = $clean_value;
last; # Exit the loop once the key is matched
last; # Exit the loop once the key is matched
}
}
}

return %metadata;
}


1;

0 comments on commit 3cca1e0

Please sign in to comment.