Chapter 5. Working with Counters
Counters provide atomic increment and decrement operations that record the count of objects.
Prerequisites
- Start the Data Grid CLI.
- Connect to a running Data Grid cluster.
5.1. Creating Counters Copy linkLink copied to clipboard!
Create strong and weak counters with the Data Grid CLI.
Procedure
- Create a CLI connection to Data Grid.
Run the
create countercommand with the appropriate arguments.Create
my-weak-counter.create counter --concurrency-level=1 --initial-value=5 --storage=PERSISTENT --type=weak my-weak-counter
create counter --concurrency-level=1 --initial-value=5 --storage=PERSISTENT --type=weak my-weak-counterCopy to Clipboard Copied! Toggle word wrap Toggle overflow Create
my-strong-counter.create counter --initial-value=3 --storage=PERSISTENT --type=strong my-strong-counter
create counter --initial-value=3 --storage=PERSISTENT --type=strong my-strong-counterCopy to Clipboard Copied! Toggle word wrap Toggle overflow
List available counters.
ls counters
ls countersCopy to Clipboard Copied! Toggle word wrap Toggle overflow Verify counter configurations.
Describe
my-weak-counter.describe counters/my-weak-counter
describe counters/my-weak-counterCopy to Clipboard Copied! Toggle word wrap Toggle overflow Copy to Clipboard Copied! Toggle word wrap Toggle overflow Describe
my-strong-counter.describe counters/my-strong-counter
describe counters/my-strong-counterCopy to Clipboard Copied! Toggle word wrap Toggle overflow Copy to Clipboard Copied! Toggle word wrap Toggle overflow
5.2. Adding Deltas to Counters Copy linkLink copied to clipboard!
Increment or decrement counters with arbitrary values.
Procedure
Select a counter.
counter my-weak-counter
counter my-weak-counterCopy to Clipboard Copied! Toggle word wrap Toggle overflow List the current count.
[//containers/default/counters/my-weak-counter]> ls 5
[//containers/default/counters/my-weak-counter]> ls 5Copy to Clipboard Copied! Toggle word wrap Toggle overflow Increment the counter value by
2.[//containers/default/counters/my-weak-counter]> add --delta=2
[//containers/default/counters/my-weak-counter]> add --delta=2Copy to Clipboard Copied! Toggle word wrap Toggle overflow Decrement the counter value by
-4.[//containers/default/counters/my-weak-counter]> add --delta=-4
[//containers/default/counters/my-weak-counter]> add --delta=-4Copy to Clipboard Copied! Toggle word wrap Toggle overflow
Strong counters return values after the operation is applied. Use --quiet=true to hide the return value.
For example, add --delta=3 --quiet=true.
Weak counters return empty responses.