• I would like to create individual temporary data for each users (registered users). I will be updating this continuously based on user’s activity. Is it OK to use Transients API? What if there are 1000+ users? Any other recommended methods? I would like to have minimum database operations.

Viewing 7 replies - 1 through 7 (of 7 total)
  • Using the Transients API can be tricky and cause you performance issues if used incorrectly. A great example of transients being a problem is the WooCommerce plugin, which uses transients to store information about a variety of things including the shopping carts that are built.

    As with any tool, there is a correct way and an incorrect way to use these things. Make sure you are setting proper expirations on the transients and that your system is actually cleaning up these expired transients. Slow MySQL performance issues will result if these are not being cleaned up correctly.

    Good luck with your project.

    Thread Starter Joel James

    (@joelcj91)

    Thanks Bob. Actually I will be deleting the Transients once the logged out. And again will create another Transient when he logged in. In a single word to track few things only when user logged in.

    Moderator bcworkz

    (@bcworkz)

    You might consider using session variables. The data will persist until the user closes their browser, which means the data could be destroyed even though the user is still logged in according to the authentication cookie. You could force the user to log in again if no session data exists, so that the session data is synched with the logged in state.
    https://php.net/manual/en/intro.session.php

    Thread Starter Joel James

    (@joelcj91)

    Session data will not be available server side. I need the live status server side.

    PHP sessions can be configured to store data in cookies, in a file, or in the DB: https://php.net/manual/en/book.session.php. The latter two are available server-side, and cookie values can be passed through AJAX requests as needed.

    Why would the session data not be available server-side? Are you trying to read/write to this info outside of users being logged in?

    Sessions can be a little tricky to understand (but well worth the trouble).

    Thread Starter Joel James

    (@joelcj91)

    Yes ancawonka. I know about sessions. I am trying to access sessions outside of users being logged in. Let me explain my actual requirement in short.

    I want the details of currently logged in users. Tried to set a flat against a user in database. But the problem is, we can’t control the status if user logged in and close the browser without logged out.

    What I am trying to do now is, create a transient for each logged in users with an expiry time. Increment the expiry on each logged in activities. So if there are no activities within last 30 mins, we will consider this user as inactive. So admin can easily get the status of currently logged in users.

    Sorry for the confusion. But, is that OK?

    Moderator bcworkz

    (@bcworkz)

    Thanks for the clarification. Transients will serve you well, IF well managed like Bob C. warned. If well managed, transients are no worse than user meta in a thousand user site. One important management aspect is to regularly clean up expired transients. WP only does this when the DB is updated – not very often. You’ll want to schedule your own task to clean up transients.

    You’ve correctly recognized that “logged in user” is not the same as “actively interacting user”. “Logged in” could be defined as the user still has a valid auth cookie on their browser. The default expiration time on auth cookies is much longer than the 30 minutes you’ve established. You might consider changing the auth cookie expiration to coincide with the transient expiration so that the site’s “logged in” matches your transient’s “logged in”. This can be done with the ‘auth_cookie_expiration’ filter. You can refresh the cookie expiration at the same time as the transient if it is done early enough that no output has yet been sent.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Is it OK if I store individual transients options for each users?’ is closed to new replies.