[Rt-commit] rt branch, 4.2/lower-level-ocfv-cache-update-code, created. rt-4.2.14-37-g9fcfeb94c

? sunnavy sunnavy at bestpractical.com
Thu Mar 22 14:08:06 EDT 2018


The branch, 4.2/lower-level-ocfv-cache-update-code has been created
        at  9fcfeb94c65639ae3fac5d1588754705a1fedfde (commit)

- Log -----------------------------------------------------------------
commit 03e65e6c8c547b84658065d69e495f53067e4b89
Author: sunnavy <sunnavy at bestpractical.com>
Date:   Fri Mar 16 04:37:48 2018 +0800

    Move OCFV cache update code to OCFV's Create/Delete methods
    
    Thus, OCFV cache is totally implemented in ObjectCustomFieldValue(s),
    other code don't need to know anything about the implementation.

diff --git a/lib/RT/CustomField.pm b/lib/RT/CustomField.pm
index ecf5907bb..62afb67cf 100644
--- a/lib/RT/CustomField.pm
+++ b/lib/RT/CustomField.pm
@@ -1843,10 +1843,6 @@ sub DeleteValueForObject {
         return ( 0, $self->loc('Input must match [_1]', $self->FriendlyPattern) );
     }
 
-    # Clear any cached values
-    my $ocfv_key = $oldval->GetOCFVCacheKey;
-    delete $RT::ObjectCustomFieldValues::_OCFV_CACHE->{$ocfv_key};
-
     # delete it
 
     my $ret = $oldval->Delete();
diff --git a/lib/RT/ObjectCustomFieldValue.pm b/lib/RT/ObjectCustomFieldValue.pm
index f5f66c12f..0a40cc45d 100644
--- a/lib/RT/ObjectCustomFieldValue.pm
+++ b/lib/RT/ObjectCustomFieldValue.pm
@@ -107,7 +107,7 @@ sub Create {
         $self->_EncodeLOB( $args{'LargeContent'}, $args{'ContentType'} )
             if defined $args{'LargeContent'};
 
-    return $self->SUPER::Create(
+    ( my $id, $msg ) = $self->SUPER::Create(
         CustomField     => $args{'CustomField'},
         ObjectType      => $args{'ObjectType'},
         ObjectId        => $args{'ObjectId'},
@@ -117,6 +117,23 @@ sub Create {
         ContentType     => $args{'ContentType'},
         ContentEncoding => $args{'ContentEncoding'},
     );
+
+    if ( $id ) {
+        my $new_value = RT::ObjectCustomFieldValue->new( $self->CurrentUser );
+        $new_value->Load( $id );
+        my $ocfv_key = $new_value->GetOCFVCacheKey();
+        if ( $RT::ObjectCustomFieldValues::_OCFV_CACHE->{$ocfv_key} ) {
+            push @{ $RT::ObjectCustomFieldValues::_OCFV_CACHE->{$ocfv_key} },
+              {
+                'ObjectId'       => $new_value->Id,
+                'CustomFieldObj' => $new_value->CustomFieldObj,
+                'Content'        => $new_value->_Value('Content'),
+                'LargeContent'   => $new_value->LargeContent,
+              };
+        }
+    }
+
+    return wantarray ? ( $id, $msg ) : $id;
 }
 
 
@@ -288,7 +305,15 @@ Disable this value. Used to remove "current" values from records while leaving t
 
 sub Delete {
     my $self = shift;
-    return $self->SetDisabled(1);
+    my ( $ret, $msg ) = $self->SetDisabled( 1 );
+    if ( $ret ) {
+        my $ocfv_key = $self->GetOCFVCacheKey();
+        if ( $RT::ObjectCustomFieldValues::_OCFV_CACHE->{$ocfv_key} ) {
+            @{ $RT::ObjectCustomFieldValues::_OCFV_CACHE->{$ocfv_key} } =
+              grep { $_->{'ObjectId'} != $self->Id } @{ $RT::ObjectCustomFieldValues::_OCFV_CACHE->{$ocfv_key} };
+        }
+    }
+    return wantarray ? ( $ret, $msg ) : $ret;
 }
 
 =head2 _FillInTemplateURL URL
diff --git a/lib/RT/Record.pm b/lib/RT/Record.pm
index 9c1ab8d75..ee460af11 100644
--- a/lib/RT/Record.pm
+++ b/lib/RT/Record.pm
@@ -2050,26 +2050,12 @@ sub _AddCustomFieldValue {
         my $new_value = RT::ObjectCustomFieldValue->new( $self->CurrentUser );
         $new_value->Load( $new_value_id );
 
-        # Prepare to update the OCFV cache
-        my $ocfv_key = $new_value->GetOCFVCacheKey;
-
         # now that adding the new value was successful, delete the old one
         if ( $old_value ) {
-            # Remove old cached value
-            @{$RT::ObjectCustomFieldValues::_OCFV_CACHE->{$ocfv_key}} =
-                grep { $_->{'ObjectId'} == $old_value->Id } @{$RT::ObjectCustomFieldValues::_OCFV_CACHE->{$ocfv_key}};
-
             my ( $val, $msg ) = $old_value->Delete();
             return ( 0, $msg ) unless $val;
         }
 
-        # Add the new one
-        push @{$RT::ObjectCustomFieldValues::_OCFV_CACHE->{$ocfv_key}}, {
-            'ObjectId'       => $new_value->Id,
-            'CustomFieldObj' => $new_value->CustomFieldObj,
-            'Content'        => $args{'Value'},
-            'LargeContent'   => $args{'LargeContent'} };
-
         if ( $args{'RecordTransaction'} ) {
             my ( $TransactionId, $Msg, $TransactionObj ) =
               $self->_NewTransaction(
@@ -2132,17 +2118,6 @@ sub _AddCustomFieldValue {
             return ( 0, $self->loc( "Could not add new custom field value: [_1]", $msg ) );
         }
 
-        my $new_value = RT::ObjectCustomFieldValue->new( $self->CurrentUser );
-        $new_value->Load( $new_value_id );
-
-        # Update the OCFV cache
-        my $ocfv_key = $new_value->GetOCFVCacheKey;
-        push @{$RT::ObjectCustomFieldValues::_OCFV_CACHE->{$ocfv_key}}, {
-            'ObjectId'       => $new_value->Id,
-            'CustomFieldObj' => $new_value->CustomFieldObj,
-            'Content'        => $args{'Value'},
-            'LargeContent'   => $args{'LargeContent'} };
-
         if ( $args{'RecordTransaction'} ) {
             my ( $tid, $msg ) = $self->_NewTransaction(
                 Type          => 'CustomField',

commit 9fcfeb94c65639ae3fac5d1588754705a1fedfde
Author: sunnavy <sunnavy at bestpractical.com>
Date:   Tue Mar 20 20:32:40 2018 +0800

    More ocfv cache tests

diff --git a/t/customfields/api.t b/t/customfields/api.t
index a50ca770c..f4551b363 100644
--- a/t/customfields/api.t
+++ b/t/customfields/api.t
@@ -2,7 +2,7 @@
 use strict;
 use warnings FATAL => 'all';
 
-use RT::Test nodata => 1, tests => 145;
+use RT::Test nodata => 1, tests => undef;
 use Test::Warn;
 
 # Before we get going, ditch all object_cfs; this will remove
@@ -223,6 +223,32 @@ warning_like {
     is($load->Id, $global_cf3->Id, "Loading by name gets non-disabled first, even with order swapped");
 }
 
+{
+    my $cf = RT::Test->load_or_create_custom_field(
+        Name  => 'HasEntry cache',
+        Type  => 'FreeformSingle',
+        Queue => 0,
+    );
+
+    my ( $ret, $msg ) = $ticket->AddCustomFieldValue( Field => $cf, Value => 'foo' );
+    ok( $ret, $msg );
+    is( $ticket->FirstCustomFieldValue( $cf ), 'foo', 'value is foo' );
+    my $ocfvs = $ticket->CustomFieldValues( $cf );
+    ok( $ocfvs->HasEntry( 'foo' ), 'foo is cached in HasEntry' );
+
+    ( $ret, $msg ) = $ticket->AddCustomFieldValue( Field => $cf, Value => 'bar' );
+    ok( $ret, $msg );
+    is( $ticket->FirstCustomFieldValue( $cf ), 'bar', 'value is bar' );
+    ok( !$ocfvs->HasEntry( 'foo' ), 'foo is not cached in HasEntry' );
+    ok( $ocfvs->HasEntry( 'bar' ),  'bar is cached in HasEntry' );
+
+    ( $ret, $msg ) = $ticket->AddCustomFieldValue( Field => $cf, Value => 'foo' );
+    ok( $ret, $msg );
+    is( $ticket->FirstCustomFieldValue( $cf ), 'foo', 'value is foo' );
+    ok( $ocfvs->HasEntry( 'foo' ),  'foo is cached in HasEntry' );
+    ok( !$ocfvs->HasEntry( 'bar' ), 'bar is not cached in HasEntry' );
+}
+
 #SKIP: {
 #       skip "TODO: should we add CF values to objects via CF Name?", 48;
 # names are not unique
@@ -230,4 +256,4 @@ warning_like {
 #       $test_add_delete_cycle->( sub { return $_[0]->Name } );
 #}
 
-
+done_testing;

-----------------------------------------------------------------------


More information about the rt-commit mailing list