11package activity_queue_item
22
33import (
4+ "context"
45 "time"
56
67 "github.com/cschleiden/go-workflows/backend/history"
@@ -23,9 +24,12 @@ func (v *Value) UpdateLastLocked() {
2324 v .LastLocked = & now
2425}
2526
27+ // Store is a storage implementation for activity queue items. StartCache must
28+ // be called before the store can be used.
2629type Store struct {
2730 client * clientv3.Client
2831 root string
32+ cache storage.Cache [* Value ]
2933}
3034
3135func NewStore (client * clientv3.Client , root string ) * Store {
@@ -47,36 +51,53 @@ func (s *Store) Key(queue, instanceID, eventID string) string {
4751 return storage .Key (s .QueuePrefix (queue ), instanceID , eventID )
4852}
4953
54+ func (s * Store ) StartCache (ctx context.Context ) error {
55+ if s .cache != nil {
56+ return nil
57+ }
58+ s .cache = storage .NewCache (s .client , s .AllQueuesPrefix (), func (item * Value ) string {
59+ return s .Key (item .Queue , item .WorkflowInstanceID , item .Event .ID )
60+ })
61+ return s .cache .Start (ctx )
62+ }
63+
64+ func (s * Store ) StopCache () {
65+ if s .cache != nil {
66+ s .cache .Stop ()
67+ }
68+ }
69+
70+ func (s * Store ) PropagateErrors (ctx context.Context , ch chan error ) {
71+ s .cache .PropagateErrors (ctx , ch )
72+ }
73+
5074func (s * Store ) GetAll () storage.GetMultipleOp [* Value ] {
51- return storage . NewGetPrefixOp [ * Value ]( s . client , s .AllQueuesPrefix ())
75+ return s . cache . GetPrefix ( s .AllQueuesPrefix ())
5276}
5377
5478func (s * Store ) GetByKey (queue , instanceID , eventID string ) storage.GetOp [* Value ] {
5579 key := s .Key (queue , instanceID , eventID )
56- return storage . NewGetOp [ * Value ]( s . client , key )
80+ return s . cache . Get ( key )
5781}
5882
5983func (s * Store ) GetByQueue (queue string ) storage.GetMultipleOp [* Value ] {
6084 prefix := s .QueuePrefix (queue )
61- return storage . NewGetPrefixOp [ * Value ]( s . client , prefix )
85+ return s . cache . GetPrefix ( prefix )
6286}
6387
6488func (s * Store ) Create (item * Value ) storage.PutOp [* Value ] {
65- key := s .Key (item .Queue , item .WorkflowInstanceID , item .Event .ID )
66- return storage .NewCreateOp (s .client , key , item )
89+ return s .cache .Create (item )
6790}
6891
6992func (s * Store ) Update (item * Value ) storage.PutOp [* Value ] {
70- key := s .Key (item .Queue , item .WorkflowInstanceID , item .Event .ID )
71- return storage .NewUpdateOp (s .client , key , item )
93+ return s .cache .Update (item )
7294}
7395
7496func (s * Store ) DeleteByKey (queue , instanceID , eventID string ) storage.DeleteOp {
7597 key := s .Key (queue , instanceID , eventID )
76- return storage . NewDeleteKeyOp ( s . client , key )
98+ return s . cache . DeleteByKey ( key )
7799}
78100
79101func (s * Store ) DeleteItem (item * Value ) storage.DeleteValueOp [* Value ] {
80- key := s .Key (item .Queue , item .WorkflowInstanceID , item .Event .ID )
81- return storage .NewDeleteValueOp (s .client , key , item )
102+ return s .cache .DeleteValue (item )
82103}
0 commit comments