HeroEngine Forums
Welcome, Guest. Please login or Register for HeroCloud Account.

Author Topic: Data Cache  (Read 1779 times)

Thazager

  • General Accounts
  • *
  • Posts: 1129
  • Never stop learning
    • View Profile
    • Heroes and Villains MMORPG
Data Cache
« on: May 05, 16, 05:42:57 PM »

I'm making a data cache for lots of data. (First time making a cache in this engine.) Currently, I use the following code sample to get the spec data each time, but I would like to cache it so that it does not have to be loaded each time I need to use it. (Having tooltips that load the data gets messy this way.)

I'm thinking of using a "list of class", and had a few questions about it.

Are the values in order of how they are entered into the list? So that if I had the key of the spec I wanted I could look it up that way? Or would it be a lookuplist indexed by list of class?

Is there a better way to cache the data? and How would I access that?


Code: [Select]
method DrawSkills()
  var skill = getSkillWindowClass()
  skillNameList as List of String = SkillClassMethods:getSkillNames(skill.skillGroup)

  // loads the current skillset data
  loop i from 1 to skillNameList.length
    offset as String = SkillClassMethods:GetSkillOffset(skillNameList[i], skill.skillGroup)  // skill name, group
    me.LoadSpecForSkill(offset)
  .
.

// call back method from server
method EventRaised( obs as NodeRef of Class ObsSubject, data as NodeRef )
  spec as NodeRef of Class EO_SkillSpec = data

  if spec != None
    me.setupSkillSlot(spec)
  .
.

method setupSkillSlot(spec as NodeRef of Class EO_SkillSpec)
  // use spec data, setup skill in window
.
« Last Edit: May 05, 16, 05:46:45 PM by Thazager »
Logged

Thazager

  • General Accounts
  • *
  • Posts: 1129
  • Never stop learning
    • View Profile
    • Heroes and Villains MMORPG
Re: Data Cache
« Reply #1 on: May 05, 16, 09:36:18 PM »

Found a way to do it.
  Made a class, as a guicontrol - SkillCache.
  Made a field, list of noderef of class SkillCache - Skill_cacheList
  Added field Skill_cacheList to E_PlayerCharacter
  Add ObsListener as parent to class SkillCache


Code: [Select]
public function ViewSkillCache()
  var gui = getMainClass()
  if gui <> None
    DestroyNode(gui)
  .
  gui = CreateNodeFromPrototype("SkillCache")
  gui.build = true

  gui.LoadLoop("TestSkill")
  gui.ShowList()
.

// used each time we change skillsets
method LoadLoop(skillset as String)
  var char = MiscUtils:getCharDataPointer()    // clear old skillset
  clear char.Skill_cacheList destroy

  if skillset <> ""
    skillNameList as List of String = SkillClassMethods:getSkillNames(skillset)

    loop i from 1 to skillNameList.length
      offset as String = SkillClassMethods:GetSkillOffset(skillNameList[i], skillset)
      me.LoadSpecForSkill(offset)
    .
  .
.

// load the spec using this key
// data is sent to function below
method LoadSpecForSkill(SpecKey as ID)
  var oracle = $SPECORACLEUTILS._GetSpecOracleByClass("SkillSpecOracle")
  oracle.requestSpecByKey(SpecKey, me)
.

// call back method from server
method EventRaised( obs as NodeRef of Class ObsSubject, data as NodeRef )
  spec as NodeRef of Class SkillSpec = data

  if spec != None
    me.saveSpecInCache(spec)
  .
  RemoveAssociation( obs, "obsSubjectToListenerAssoc", me )
.

method saveSpecInCache(spec as NodeRef of Class SkillSpec)
  var char = MiscUtils:getCharDataPointer()

  newList as List of NodeRef of Class SkillSpec = char.Skill_cacheList

  add back spec to newList            // add data 1 spec at a time

  char.Skill_cacheList = newList   // save data
.

method ShowList()
  var char = MiscUtils:getCharDataPointer()

  newList as List of NodeRef of Class SkillSpec = char.Skill_cacheList

  foreach val in newList
    println("val "+val.displayName)
  .
.

// get main window class for variables
function getMainClass() as NodeRef of Class SkillCache
  return FindGuiControlByName(None,"default.SkillCache")
.
« Last Edit: May 05, 16, 10:04:02 PM by Thazager »
Logged

FI-ScottZ

  • General Accounts
  • *
  • Posts: 1405
    • View Profile
    • Forever Interactive, Inc.
Re: Data Cache
« Reply #2 on: May 06, 16, 10:18:38 AM »

Now that I have had time to read this and understand what you are doing, unfortunately what you wrote was all unnecessary.

With specs on the client side, it is pointed out in documentation that if you store a ref to a spec node there is a risk of it becoming invalid if the spec is changed in the meantime.  That is why they recommend using RequestSpecByKey() rather than GetSpecByKey().  As you can see inside of those methods, the client-side specs are already being cached when received.

So, looking inside of RequestSpecByKey() you will see that you can safely call it every time you want to use the spec (as you did in your first post) and it will only load it from the server when needed.  If it has already been loaded, and not changed since then, it will give you the noderef from its own client-side cache.
Logged
Scott Zarnke
Lead Programmer, Visions of Zosimos
CTO, Forever Interactive, Inc.

Thazager

  • General Accounts
  • *
  • Posts: 1129
  • Never stop learning
    • View Profile
    • Heroes and Villains MMORPG
Re: Data Cache
« Reply #3 on: May 06, 16, 07:16:49 PM »

I'm not sure I see the client side cache, in order to get the noderef.

How would I access it, and which field do I use? I'm not seeing any way of it being stored.

This might load it, but does not seem to be putting it anyplace to "access later".

Code: [Select]
  loop i from 1 to skillNameList.length
    offset as String = SkillClassMethods:GetSkillOffset(skillNameList[i], skill.skillGroup)  // skill name, group
    me.LoadSpecForSkill(offset)
  .


This being a preset method (not able to change the fields), does not seem to "save" it anywhere. While it does pull in the values of the spec, in order to get another value, I would have to run both methods again. Which does not work for what we need.

Code: [Select]
method EventRaised( obs as NodeRef of Class ObsSubject, data as NodeRef )
  spec as NodeRef of Class EO_SkillSpec = data

  if spec != None
    me.setupSkillSlot(spec)
  .
.
Logged

FI-ScottZ

  • General Accounts
  • *
  • Posts: 1405
    • View Profile
    • Forever Interactive, Inc.
Re: Data Cache
« Reply #4 on: May 07, 16, 05:32:21 AM »

So, do you object to the loading or the fact that it is asynchronous?

The SpecOracle class method RequestSpecByKey() will request the spec to be loaded if it was not already via requestSpecData(). Then in the callback method onRepositoryDataDownloaded() it stores the spec node in its lookuplist field _SpecKeyMap, as well as in an associated collection.  That is the cache it reads from the next time you request that spec.

If it is the fact that you receive it in the method EventRaised you don't like, well that is the only way that mechanism works and is the safest method.  Alternatively, you could do the request one time at the beginning to be sure it gets loaded, and later access the spec via the spec oracle's method GetSpecByKey().  That one is synchronous and so returns the spec from the cache immediately as a return value.  But then you run the risk of it not returning a node if the spec has been invalidated, though that is the same risk if you directly accessed your own cache.
Logged
Scott Zarnke
Lead Programmer, Visions of Zosimos
CTO, Forever Interactive, Inc.

Thazager

  • General Accounts
  • *
  • Posts: 1129
  • Never stop learning
    • View Profile
    • Heroes and Villains MMORPG
Re: Data Cache
« Reply #5 on: May 11, 16, 04:49:08 PM »

So, do you object to the loading or the fact that it is asynchronous?
While this works for the majority of what we need. It does not work for the tooltips we create.

Perhaps I could try GetSpecByKey() for those.

Thanks
Logged

ToY-Krun

  • General Accounts
  • *
  • Posts: 677
  • Support Volunteer
    • View Profile
Re: Data Cache
« Reply #6 on: May 11, 16, 07:36:06 PM »

Im not sure i follow precisely, but, for ours, we store the tooltip on the icon or other control, while the spec is loaded and we populate the icon/control.  for example, inventory icons contain the itemID, tool tip description, name etc of the item it represents.  could you not do the same for your tooltips?

Thazager

  • General Accounts
  • *
  • Posts: 1129
  • Never stop learning
    • View Profile
    • Heroes and Villains MMORPG
Re: Data Cache
« Reply #7 on: May 17, 16, 12:59:57 AM »

that also might be a possibility.
Logged

Thazager

  • General Accounts
  • *
  • Posts: 1129
  • Never stop learning
    • View Profile
    • Heroes and Villains MMORPG
Re: Data Cache
« Reply #8 on: Aug 11, 16, 07:15:15 PM »

... we store the tooltip on the icon or other control...
This is our tooltip

Logged