Cache data

2020-12-17
2020-12-22
  • andras-mozes - 2020-12-17

    Hello,

    What would be an ideal solution to save data before processing them.

    I mean I am reading strUserID and I want to persist them before saving them to a MySQL database.

    Actually I would like to use 2 tasks. One collecting strUserID while another one handling MySQL connection and sending of them.

    The problem that I would like to save the strUserID before sending them thus in case of power outage, I didn't lose any strUserID.

    I don't know whether to use collections (persisted) or persistent manager (as txt) or var persistent?

    Thanks for your help in advance.

    BR!

     
  • i-campbell

    i-campbell - 2020-12-22

    If the requirement is really not to lose any then the file-based persistence manager may be too slow for you.
    Use a controller with non-volatile ram setup for retain data. Then use a circular array stored in VAR PERSISTENT. The array size should be as big as the maximum number of userids you intend to buffer.

     
    πŸ‘
    1
    • andras-mozes - 2020-12-22
      {attribute 'qualified_only'}
      VAR_GLOBAL PERSISTENT RETAIN
          aAuthData : ARRAY[0..300] OF AuthData;
      END_VAR
      

      How could I set-up a circular array?
      Would you mind showing some resources, examples? :)

      Thanks for your help in advance.

       
  • i-campbell

    i-campbell - 2020-12-22
    VAR_GLOBAL PERSISTENT RETAIN
        aAuthData : ARRAY[0..300] OF AuthData;
        udiInsertIndex : UDINT := 300;
        udiReadIndex : UDINT := 300;
        processed : BOOL := TRUE;
    END_VAR
    
    //Adding a value:
    IF xNewData THEN
        udiInsertIndex := (udiInsertIndex+1) MOD 301;
        aAuthData[udiInsertIndex] := newAuthData;
        xNewData := FALSE;
    END_IF
    
    //Checking for new values:
    IF udiReadIndex <> udiInsertIndex AND processed THEN
        udiReadIndex := (udiReadIndex+1) MOD 301;
        processed := FALSE;
    END_IF
    
    //Process the new value
    IF NOT processed THEN
        //Process the value and set processed when finished
        DoSomethingWith(aAuthData[udiReadIndex], processed);
    END_IF
    
     
    πŸ‘
    1

Log in to post a comment.