Sorry, I thought I had shown the relevant parts of the code, but I see now I left out how I get the object. Pretty important part.
The complete code is as follows:
$id = $modx->resource->id;
$count = 0; // Needs to be a number if new page
$tv = $modx->getObject('modTemplateVar', array('name'=>'docHits'));
$count = $tv->getValue($id);
$count++;
$tv->setValue($id,$count);
$tv->save();
return;
I run it from the content of the page like [ [!MyPageCounter]]. Same things happens if I run as plugin but to make it easy i here run it as a snippet.
In the example below I show the TV value as in database, to be sure it isn't a display problem. (On the page I show the tv value, as I usually run this as a plugin. But when it works I'll change to a snippet instead as BobRay suggested it will be faster. Again, the values here are from database, this is not a display error.)
Let´s say my counter is at 100.
This is what happens if the page from where it is called is CACHED:
I empty the cache manually.
Reload the page
The TV value (checked in database) is now 101, perfect.
I reload the page. The tvvalue in db is still 101.
Reloads 5 times. Value still 101.
Clears cache, reloads page. Value now 102! Nice.
Reloads 5 times. Value still 102.
Clears cache. Reloads. Value 103.
And so on.
Ok, let's make the PAGE from where the snippet is called UNcached:
Reload. Value 104. Reload: 105.
Reloads 5 times: 106,107,108, 109, 110.
Working. But I can't have the page uncached.
Perhaps I should try using getTVvalue instead? But then I have to first get the resource object, seems not that efficient?
This code worked fine in 2.08.
I wonder what I'm doing wrong, the time I've spent on this little problem is the same as I use making two complete sites.